How to configure automatic log deletion in Grafana Loki with filesystem storage in Docker?
I’ve tried various options from the official Grafana Loki documentation (retention., delete.) but they don’t work. Please advise on how to properly enable automatic deletion of old logs in my configuration:
loki-config.yaml:
auth_enabled: false
server:
http_listen_port: 3100
grpc_listen_port: 9096
log_level: debug
grpc_server_max_concurrent_streams: 1000
common:
instance_addr: 127.0.0.1
path_prefix: /tmp/loki
storage:
filesystem:
chunks_directory: /tmp/loki/chunks
rules_directory: /tmp/loki/rules
replication_factor: 1
ring:
kvstore:
store: inmemory
query_range:
parallelise_shardable_queries: true
align_queries_with_step: true
results_cache:
cache:
embedded_cache:
enabled: true
max_size_mb: 100
limits_config:
metric_aggregation_enabled: true
reject_old_samples: true
reject_old_samples_max_age: 10s
retention_period: 10s
compactor:
working_directory: /loki/compactor
compaction_interval: 10s
retention_enabled: true
retention_delete_delay: 0s
delete_request_store: filesystem
delete_request_cancel_period: 10s
delete_max_interval: 10s
table_manager:
retention_deletes_enabled: true
schema_config:
configs:
- from: 2020-10-24
store: tsdb
object_store: filesystem
schema: v13
index:
prefix: index_
period: 24h
pattern_ingester:
enabled: true
metric_aggregation:
loki_address: localhost:3100
flush_check_period: 1m
ruler:
alertmanager_url: http://localhost:9093
frontend:
encoding: protobuf
docker-compose.yaml:
services:
loki:
image: grafana/loki
container_name: loki
command: -config.file=/etc/loki/config/loki-config.yaml
volumes:
- ./loki-config.yaml:/etc/loki/config/loki-config.yaml
- ./loki-data:/tmp/loki
ports:
- “3100:3100”
grafana:
image: grafana/grafana
container_name: grafana
ports:
- “3000:3000”
volumes:
- ./grafana-data:/var/lib/grafana
depends_on:
- loki
How to properly configure retention period and automatic log deletion in Grafana Loki with filesystem storage in Docker?
The problem with automatic log deletion in Grafana Loki with filesystem storage in Docker arises due to configuration mismatch. Here’s how to properly configure automatic deletion of old logs:
For proper automatic log deletion in Grafana Loki with filesystem storage in Docker, you need to perform the following settings:
limits_config:
retention_period: 720h # Minimum 24 hours (e.g., 30 days = 720h)
# ... other settings
compactor:
working_directory: /tmp/loki/retention # Outside chunks_directory
compaction_interval: 10m
retention_enabled: true
retention_delete_delay: 2h # Delay before deletion
retention_delete_worker_count: 150
delete_request_store: filesystem # Important for filesystem
# ... other settings
schema_config:
configs:
- from: 2020-10-24
store: tsdb
object_store: filesystem
schema: v13
index:
period: 24h # Must be a multiple of retention_period
Main issues in your configuration:
-
Incorrect retention period:
retention_period: 10sis less than the minimum possible 24 hours- Must be a multiple of the indexing period (24h)
-
Missing required parameters:
- Missing
retention_delete_worker_count retention_delete_delayis set to 0s (recommended minimum 1-2 hours)
- Missing
-
Incorrect structure:
delete_request_storeis specified but not used inlimits_configworking_directoryshould be a separate directory, not coinciding with chunks_directory
Corrected configuration:
loki-config.yaml:
limits_config:
retention_period: 720h # 30 days
# ... other settings
compactor:
working_directory: /tmp/loki/retention
compaction_interval: 10m
retention_enabled: true
retention_delete_delay: 2h
retention_delete_worker_count: 150
delete_request_store: filesystem
# ... other settings
schema_config:
configs:
- from: 2020-10-24
store: tsdb
object_store: filesystem
schema: v13
index:
period: 24h
Docker configuration for retention:
docker-compose.yaml:
services:
loki:
image: grafana/loki:latest
volumes:
- ./loki-config.yaml:/etc/loki/config/loki-config.yaml
- loki-data:/tmp/loki
- loki-retention:/tmp/loki/retention # Separate volume for compactor
# ... other settings
volumes:
loki-data:
loki-retention: # Required for retention
Verification:
- Ensure the retention directory exists and is writable:
mkdir -p ./loki-data/retention
chmod 755 ./loki-data/retention
- Restart the container:
docker-compose down && docker-compose up -d
- Check logs for errors:
docker-compose logs loki
- After 24 hours, observe deletion of old logs through Grafana or Loki CLI
Important notes:
- Minimum retention period: 24 hours
- Period multiples:
retention_periodmust be a multiple ofindex.period - Loki version: Use recent versions (v2.8+ or v3.x)
- Deletion granularity: Logs are deleted in batches after compaction
Common errors:
- Missing separate volume for retention
- Incorrect directory permissions
- Too small
retention_delete_delay - Insufficient space for compactor operation
After proper configuration, old logs will be automatically deleted after the specified retention period. For monitoring, use Grafana with Loki metrics dashboards (log_entries_total).