This guide shows you how to set up log retention in Grafana Loki, the log storage system, so old logs get deleted on a schedule. It is for anyone running Loki whose storage bucket keeps growing. Loki keeps every chunk (the compressed blocks it stores log lines in) forever until you turn retention on. The deleting is done by one component, the compactor, a background job that also tidies Loki’s index. A bucket lifecycle rule, the ingesters (the parts that receive incoming logs) and a cron job play no part in it.
The fix is almost always three settings. Run exactly one compactor, set retention_enabled: true in its config, and give it a retention_period (how long to keep logs). This guide walks through each step. You will enable the compactor, set a global retention window, and add rules for specific log streams and tenants (separate teams or customers sharing one Loki). Then you restart and confirm from the logs and metrics that expired chunks are marked and swept away. Retention requires a TSDB or boltdb-shipper index, the two index formats that support it.
What you’ll accomplish
- Run a single compactor with retention enabled, on a schedule you control.
- Set a global
retention_periodso every stream ages out at a known age. - Override that per stream and per tenant, with selectors and priority you can reason about.
- Read the compactor’s logs and
loki_compactor_*metrics to confirm retention is actually running. - Verify from the object store that old chunks disappear, not just that the config parses.
Prerequisites
- A running Grafana Loki, single-binary or simple-scalable, version 2.9 or newer (3.x is assumed for a couple of config keys and called out where it matters). Retention behaves the same across these deployment modes as long as one compactor owns the job.
- A supported store using the TSDB or boltdb-shipper index. Retention does not work with the legacy chunk-store index types or with a store schema older than
v11. If you are on an older schema, add a newv13(TSDB) schema period going forward before you expect retention to apply. - Write access to the Loki config file and the ability to restart the process.
- Object or filesystem storage you can inspect (S3, GCS, Azure Blob, or a local
filesystemstore), so you can confirm chunks are removed. - An honest caveat: retention only happens when the compactor runs with
retention_enabled: true, and only one compactor may run against a given store. There is no per-ingester or per-query deletion. If you take nothing else from this page, take that.
How it works
The compactor runs one loop on an interval. First it compacts the index — it merges the many small per-table index files the ingesters produce into one file per table, which is what keeps queries fast. Then, if retention is enabled, the same loop reads that index and marks every chunk whose age exceeds the applicable retention window. Marked chunks are not deleted immediately; they sit for a delete_delay so an in-flight query does not lose data under it, and a later pass sweeps them from the store. The store shrinks only at that sweep, not when the mark happens.
Two settings decide what gets marked. retention_period in limits_config is the global default — every stream ages out at that age unless something more specific overrides it. Per-stream rules, written as retention_stream entries with a LogQL selector and their own period, override the global for streams they match; per-tenant overrides live in the overrides file and can set both a tenant default and tenant-scoped stream rules. When more than one retention_stream rule matches a stream, the one with the highest priority wins, and ties fall back to the longest period. Separately, delete_request_store tells the compactor where to persist tenant-issued delete requests (the Loki delete API), which the same loop processes alongside age-based retention.
Step-by-step
1. Enable the compactor with retention on. Add or edit the compactor block. retention_enabled: true is the switch that turns marking on; without it the compactor only compacts the index and deletes nothing.
compactor:
working_directory: /var/loki/compactor
compaction_interval: 10m
retention_enabled: true
retention_delete_delay: 2h
delete_request_store: filesystem
working_directory must be writable and local to the compactor. delete_request_store names a store type (filesystem, s3, gcs, …) and is required in Loki 3.x whenever retention_enabled is true — the process refuses to start otherwise, which is the first success signal:
level=info msg="compactor started"
2. Set the global retention period. Put a default in limits_config. This is the age at which any stream is eligible for deletion.
limits_config:
retention_period: 744h
Use hours — 744h is 31 days. 0s means keep forever. On restart the value is echoed in the config the compactor loads; a quick way to confirm it took is the metrics check in step 6.
3. Add per-stream retention rules. Keep most data short and a few streams longer (or the reverse) with retention_stream entries. Each has a LogQL selector, a period, and a priority.
limits_config:
retention_period: 744h
retention_stream:
- selector: '{namespace="ingress"}'
priority: 1
period: 168h
- selector: '{namespace="audit"}'
priority: 2
period: 8760h
Higher priority wins when two selectors match the same stream, so give the more specific rule the higher number. Here ingress logs age out at 7 days, audit logs are kept a year, everything else falls to the 31-day global.
4. Add per-tenant overrides (multi-tenant only). If you run Loki with multiple tenants, per-tenant retention lives in the runtime overrides file referenced by runtime_config. Each tenant can carry its own retention_period and its own retention_stream list.
overrides:
team-a:
retention_period: 2160h
retention_stream:
- selector: '{level="debug"}'
priority: 10
period: 72h
This file is re-read on the runtime_config reload interval, so tenant retention changes do not need a restart.
5. Restart Loki and confirm one compactor. Restart the process (or roll the deployment). In simple-scalable mode make sure the compactor target runs on exactly one instance.
sudo systemctl restart loki
6. Watch the compactor apply retention. Tail the logs. After the compaction phase you want to see retention lines — marking is logged explicitly:
journalctl -u loki -f | grep -i retention
A healthy loop looks like this:
level=info msg="applying retention with compaction"
level=info msg="marked chunks for deletion" table=index_19934 marked=1284
Then confirm from metrics. Curl the compactor’s /metrics endpoint and look at the retention counters:
curl -s http://127.0.0.1:3100/metrics | grep -E 'loki_compactor_(deleted_chunks|pending_delete)'
You want loki_compactor_deleted_chunks_total climbing over successive loops:
loki_compactor_deleted_chunks_total 1284
Validate it works
Confirm exactly one compactor is running. This is the most common way retention goes wrong. On the ring page (/compactor/ring on a distributed deployment) you should see a single ACTIVE entry. On single-binary, confirm no second process is pointed at the same store. Two compactors racing the same index will corrupt it.
Confirm the retention counters advance. Over two or three compaction_interval cycles, loki_compactor_deleted_chunks_total and the boltdb-shipper marker metric (loki_boltdb_shipper_retention_marker_count_total, present on boltdb-shipper deployments) should keep rising. A flat counter after retention was enabled means nothing is being marked — jump to Troubleshooting.
Confirm chunks leave the store. After a full cycle plus retention_delete_delay, check the store directly. For a filesystem store:
du -sh /var/loki/chunks
For S3, compare object counts under the chunks prefix across a day. The number should fall for tables past their retention window, not just stop growing. If the index compacts but chunk bytes never drop, the sweep is not completing — check retention_delete_delay has elapsed and the compactor has write access to the store.
Troubleshooting
Retention parses but nothing is deleted. Three usual causes, in order of likelihood: retention_enabled is still false (or absent, which is false); no compactor is actually running the retention target; or more than one compactor is running and they are interfering. Confirm the compactor started and applying retention log lines both appear on one and only one instance.
Wrong index schema. Retention needs a TSDB or boltdb-shipper index at schema v11, v12, or v13. On an older schema_config period you will see compaction but no retention effect. Add a new schema period with store: tsdb and schema: v13 starting from a future date; retention applies to data written under the new period.
A per-stream rule never matches. The selector is LogQL and must match labels that exist on the stream exactly — a typo, a label that was never set, or a selector that is more general than a competing higher-priority rule all leave it inert. Check the actual stream labels in Grafana, then confirm no other retention_stream with a higher priority is shadowing it. Remember the global retention_period applies to anything no rule matches.
Disk still grows after enabling retention. Either the loop is not finishing (a very long compaction_interval on a large index) or the sweep is still inside retention_delete_delay. Deletes are intentionally lagged. Give it at least one interval plus the delay before concluding it failed, and check the logs for a stuck or repeating compaction of the same table.
Two compactors corrupt the index. If you scaled the compactor target to more than one replica, or a leftover single-binary is still pointed at the store, they will both mark and sweep and can leave the index inconsistent. Scale the compactor to one, and if the index is already damaged, stop all writers and let a single compactor recompact before resuming queries.
When this is the wrong approach
If all you need is a flat cap — delete everything older than N days with no per-stream nuance — an object-store lifecycle policy on the chunks prefix is simpler and needs no Loki config. It will not touch the index and will not honor per-stream rules, so use it only as a blunt safety net alongside, not instead of, the compactor. For an ephemeral dev cluster you tear down regularly, retention is overhead you do not need; let it grow and delete the bucket. And if you have a genuine long-term compliance requirement — years of audit logs that must be provably retained and immutable — Loki retention is the wrong layer for the guarantee; keep those logs in a store with object-lock or WORM controls and treat Loki as the queryable hot tier.
For the authoritative reference, see the Loki retention documentation. To get logs flowing in the first place, see how to ship logs to Loki with Grafana Alloy or build a Vector log pipeline.
If alerts fire into an empty room after hours, that’s exactly what 24/7 Monitoring & Response closes — book a monitoring and response review.