Project: Grafana Loki (Observability and Analysis / Observability)
File: retention.md

title: Log retention menuTitle: Log retention description: Describes how Loki implements storage retention and retention configuration options. weight: 600 Retention in Grafana Loki is achieved through the . By default the `compactor.retention-enabled` flag is not set, so the logs sent to Loki live forever. {{% admonition type="note" %}} If you have a lifecycle policy configured on the object store, please ensure that it is longer than the retention period. {{% /admonition %}} Granular retention policies to apply retention at per tenant or per stream level are also supported by the Compactor. {{% admonition type="note" %}} The Compactor does not support retention on . Please use the when using legacy index types. Both the Table manager and legacy index types are deprecated and may be removed in future major versions of Loki. {{% /admonition %}} The Compactor is responsible for compaction of index files and applying log retention. {{% admonition type="note" %}} Run the Compactor as a singleton (a single instance). {{% /admonition %}} The Compactor loops to apply compaction and retention at every `compactor.compaction-interval`, or as soon as possible if running behind. Both compaction and retention are idempotent. If the Compactor restarts, it will continue from where it left off. The Compactor's algorithm to apply retention is as follows: For each day or table (one table per day with 24h index period): Compact multiple index files in the table into per-tenant index files. Result of compaction is a single index file per tenant per day. Traverse the per-tenant index. Use the tenant configuration to identify the chunks that need to be removed. Remove the references to the matching chunks from the index and add the chunk references to a marker file on disk. Upload the new modified index files. Chunks are not deleted while applying the retention algorithm on the index. They are deleted asynchronously by a sweeper process and this delay can be configured by setting `-compactor.retention-delete-delay`. Marker files are used to keep track of the chunks pending for deletion. Chunks cannot be deleted immediately for the following reasons: Index Gateway downloads a copy of the index files to serve queries and refreshes them at a regular interval. Having a delay allows the index gateways to pull the modified index file which would not contain any reference to the chunks marked for deletion. Without the delay, index files (that are stale) on the gateways could refer to already deleted chunks leading to query failures. It provides a short window of time in which to cancel chunk deletion in the case of a configuration mistake. Marker files should be stored on a persistent disk to ensure that the chunks pending for deletion are processed even if the Compactor process restarts. {{% admonition type="note" %}} Grafana Labs recommends running Compactor as a stateful deployment (StatefulSet when using Kubernetes) with a persistent storage for storing marker files. {{% /admonition %}} This Compactor configuration example activates

```yaml compactor: working_directory: /data/retention compaction_interval: 10m retention_enabled: true retentiondeletedelay: 2h retentiondeleteworker_count: 150 deleterequeststore: gcs schema_config: configs: from: "2020-07-31" index: period: 24h prefix: index_ object_store: gcs schema: v13 store: tsdb storage_config: tsdb_shipper: activeindexdirectory: /data/index cachelocation: /data/indexcache gcs: bucket_name: loki ``` {{% admonition type="note" %}} Retention is only available if the index period is 24h. Single store TSDB and single store BoltDB require 24h index period. {{% /admonition %}} `retention_enabled` should be set to true. Without this, the Compactor will only compact tables. `deleterequeststore` should be set to configure the store for delete requests. This is required when retention is enabled. `working_directory` is the directory where marked chunks and temporary tables will be saved. `compaction_interval` dictates how often compaction and/or retention is applied. If the Compactor falls behind, compaction and/or retention occur as soon as possible. `retentiondeletedelay` is the delay after which the Compactor will delete marked chunks. `retentiondeleteworker_count` specifies the maximum quantity of goroutine workers instantiated to delete chunks. Retention period is configured within the configuration section. There are two ways of setting retention policies: `retention_period` which is applied globally for all log streams. `retention_stream` which is only applied to log streams matching the selector. {{% admonition type="note" %}} The minimum retention period is 24h. {{% /admonition %}} This example configures global retention that applies to all tenants (unless overridden by configuring per-tenant overrides): ```yaml ... limits_config: retention_period: 744h retention_stream: selector: '{namespace="dev"}' priority: 1 period: 24h pertenantoverride_config: /etc/overrides.yaml ... ``` {{% admonition type="note" %}} You can only use label matchers in the `selector` field of a `retention_stream` definition. Arbitrary LogQL expressions are not supported. {{% /admonition %}} Per tenant retention can be defined by configuring . For example: ```yaml overrides: "29": retention_period: 168h retention_stream: selector: '{namespace="prod"}' priority: 2 period: 336h selector: '{container="loki"}' priority: 1 period: 72h "30": retention_stream: selector: '{container="nginx", level="debug"}' priority: 1 period: 24h ``` Retention period for a given stream is decided based on the first match in this list: If mutiple per-tenant `retention_stream` selectors match the stream, retention period with the highest priority is picked. If multiple global `retentionstream` selectors match the stream, retention period with the highest priority is picked. This value is not considered if per-tenant `retentionstream` is set. If a per-tenant `retention_period` is specified, it will be applied. The global `retention_period` will be applied if none of the above match. If no global `retention_period` is specified, the default value of `744h` (30days) retention is used. {{% admonition type="note" %}} The larger the priority value, the higher the priority. {{% /admonition %}} Stream matching uses the same syntax as Prometheus label matching: `=`: Select labels that are exactly equal to the provided string. `!=`: Select labels that are not equal to the provided string. `=~`: Select labels that regex-match the provided string. `!~`: Select labels that do not regex-match the provided

The example configurations defined above will result in the following retention periods: For tenant `29`: Streams that have the namespace label `prod` will have a retention period of `336h` (2 weeks), even if the container label is `loki`, since the priority of the `prod` rule is higher. Streams that have the container label `loki` but are not in the namespace `prod` will have a `72h` retention period. For the rest of the streams in this tenant, per-tenant override `retention_period` value of `168h` is applied. For tenant `30`: Streams that have the label `nginx` and level `debug` will have a retention period of `24h`. For the rest of the streams in this tenant the global retention period of `744h`, since there is no override specified. All tenants except `29` and `30`: Streams that have the namespace label `dev` will have a retention period of `24h` hours. Streams except those with the namespace label `dev` will have the retention period of `744h`. Retention through the is achieved by relying on the object store TTL feature, and will work for both store and chunk/index stores. In order to enable the retention support, the Table Manager needs to be configured to enable deletions and a retention period. Please refer to the section of the Loki configuration reference for all available options. Alternatively, the `table-manager.retention-period` and `table-manager.retention-deletes-enabled` command line flags can be used. The provided retention period needs to be a duration represented as a string that can be parsed using the Prometheus common model . Examples: `7d`, `1w`, `168h`. {{% admonition type="warning" %}} The retention period must be a multiple of the index and chunks table `period`, configured in the block. See the documentation for more information. {{% /admonition %}} {{% admonition type="note" %}} To avoid querying of data beyond the retention period,`maxquerylookback` config in must be set to a value less than or equal to what is set in `tablemanager.retentionperiod`. {{% /admonition %}} When using S3 or GCS, the bucket storing the chunks needs to have the expiry policy set correctly. For more details check or . The retention policy for Table manager can only be set globally. Per-tenant and per-stream retention policies along with support for deleting ingested logs using an API are only supported by Compactor retention. Since a design goal of Loki is to make storing logs cheap, a volume-based deletion API is deprioritized. Until this feature is released, if you suddenly must delete ingested logs, you can delete old chunks in your object store. Note, however, that this only deletes the log content and keeps the label index intact; you will still be able to see related labels but will be unable to retrieve the deleted log content. For further details on the Table Manager internals, refer to the documentation. Example configuration with GCS with a 28 day retention: ```yaml schema_config: configs: from: 2018-04-15 store: tsdb object_store: gcs schema: v13 index: prefix: lokiindex period: 24h storage_config: tsdb_shipper: activeindexdirectory: /loki/index cachelocation: /loki/indexcache gcs: bucketname: GCSBUCKET_NAME limits_config: maxquerylookback: 672h # 28 days retention_period: 672h # 28 days compactor: working_directory: /data/retention deleterequeststore: gcs retention_enabled: true ```