github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/docs/sources/operations/storage/retention.md (about)

     1  ---
     2  title: Retention
     3  ---
     4  # Grafana Loki Storage Retention
     5  
     6  Retention in Grafana Loki is achieved either through the [Table Manager](#table-manager) or the [Compactor](#compactor).
     7  
     8  By default, when `table_manager.retention_deletes_enabled` or `compactor.retention_enabled` flags are not set, then logs sent to Loki live forever.
     9  
    10  Retention through the [Table Manager](../table-manager/) is achieved by relying on the object store TTL feature, and will work for both [boltdb-shipper](../boltdb-shipper) store and chunk/index store. However retention through the [Compactor](../boltdb-shipper#compactor) is supported only with the [boltdb-shipper](../boltdb-shipper) store.
    11  
    12  The Compactor retention will become the default and have long term support. It supports more granular retention policies on per tenant and per stream use cases.
    13  
    14  ## Compactor
    15  
    16  The [Compactor](../boltdb-shipper#compactor) can deduplicate index entries. It can also apply granular retention. When applying retention with the Compactor, the [Table Manager](../table-manager/) is unnecessary.
    17  
    18  > Run the compactor as a singleton (a single instance).
    19  
    20  Compaction and retention are idempotent. If the compactor restarts, it will continue from where it left off.
    21  
    22  The Compactor loops to apply compaction and retention at every `compaction_interval`, or as soon as possible if running behind.
    23  
    24  The compactor's algorithm to update the index:
    25  
    26  - For each table within each day:
    27    - Compact the table into a single index file.
    28    - Traverse the entire index. Use the tenant configuration to identify and mark chunks that need to be removed.
    29    - Remove marked chunks from the index and save their reference in a file on disk.
    30    - Upload the new modified index files.
    31  
    32  The retention algorithm is applied to the index. Chunks are not deleted while applying the retention algorithm. The chunks will be deleted by the compactor asynchronously when swept.
    33  
    34  Marked chunks will only  be deleted after `retention_delete_delay` configured is expired because:
    35  
    36  - boltdb-shipper indexes are refreshed from the shared store on components using it (querier and ruler) at a specific interval. This means deleting chunks instantly could lead to components still having reference to old chunks and so they could fails to execute queries. Having a delay allows for components to refresh their store and so remove gracefully their reference of those chunks.
    37  
    38  - It provides a short window of time in which to cancel chunk deletion in the case of a configuration mistake.
    39  
    40  Marker files (containing chunks to delete) should be stored on a persistent disk, since the disk will be the sole reference to them.
    41  
    42  ### Retention Configuration
    43  
    44  This compactor configuration example activates retention.
    45  
    46  ```yaml
    47  compactor:
    48    working_directory: /data/retention
    49    shared_store: gcs
    50    compaction_interval: 10m
    51    retention_enabled: true
    52    retention_delete_delay: 2h
    53    retention_delete_worker_count: 150
    54  schema_config:
    55      configs:
    56        - from: "2020-07-31"
    57          index:
    58              period: 24h
    59              prefix: loki_index_
    60          object_store: gcs
    61          schema: v11
    62          store: boltdb-shipper
    63  storage_config:
    64      boltdb_shipper:
    65          active_index_directory: /data/index
    66          cache_location: /data/boltdb-cache
    67          shared_store: gcs
    68      gcs:
    69          bucket_name: loki
    70  ```
    71  
    72  > Note that retention is only available if the index period is 24h.
    73  
    74  Set `retention_enabled` to true. Without this, the Compactor will only compact tables.
    75  
    76  Define `schema_config` and `storage_config` to access the storage.
    77  
    78  The index period must be 24h.
    79  
    80  `working_directory` is the directory where marked chunks and temporary tables will be saved.
    81  
    82  `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.
    83  
    84  `retention_delete_delay` is the delay after which the compactor will delete marked chunks.
    85  
    86  `retention_delete_worker_count` specifies the maximum quantity of goroutine workers instantiated to delete chunks.
    87  
    88  #### Configuring the retention period
    89  
    90  Retention period is configured within the [`limits_config`](./../../../configuration/#limits_config) configuration section.
    91  
    92  There are two ways of setting retention policies:
    93  
    94  - `retention_period` which is applied globally.
    95  - `retention_stream` which is only applied to chunks matching the selector
    96  
    97  > The minimum retention period is 24h.
    98  
    99  This example configures global retention:
   100  
   101  ```yaml
   102  ...
   103  limits_config:
   104    retention_period: 744h
   105    retention_stream:
   106    - selector: '{namespace="dev"}'
   107      priority: 1
   108      period: 24h
   109    per_tenant_override_config: /etc/overrides.yaml
   110  ...
   111  ```
   112  
   113  Per tenant retention can be defined using the `/etc/overrides.yaml` files. For example:
   114  
   115  ```yaml
   116  overrides:
   117      "29":
   118          retention_period: 168h
   119          retention_stream:
   120          - selector: '{namespace="prod"}'
   121            priority: 2
   122            period: 336h
   123          - selector: '{container="loki"}'
   124            priority: 1
   125            period: 72h
   126      "30":
   127          retention_stream:
   128          - selector: '{container="nginx"}'
   129            priority: 1
   130            period: 24h
   131  ```
   132  
   133  A rule to apply is selected by choosing the first in this list that matches:
   134  
   135  1. If a per-tenant `retention_stream` matches the current stream, the highest priority is picked.
   136  2. If a global `retention_stream` matches the current stream, the highest priority is picked.
   137  3. If a per-tenant `retention_period` is specified, it will be applied.
   138  4. The global `retention_period` will be selected if nothing else matched.
   139  5. If no global `retention_period` is specified, the default value of `744h` (30days) retention is used.
   140  
   141  Stream matching uses the same syntax as Prometheus label matching:
   142  
   143  - `=`: Select labels that are exactly equal to the provided string.
   144  - `!=`: Select labels that are not equal to the provided string.
   145  - `=~`: Select labels that regex-match the provided string.
   146  - `!~`: Select labels that do not regex-match the provided string.
   147  
   148  The example configurations will set these rules:
   149  
   150  - All tenants except `29` and `30` in the `dev` namespace will have a retention period of `24h` hours.
   151  - All tenants except `29` and `30` that are not in the `dev` namespace will have the retention period of `744h`.
   152  - For tenant `29`:
   153    - All streams except those in the container `loki` or in the namespace `prod` will have retention period of `168h` (1 week).
   154    - All streams in the `prod` namespace 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.
   155    - Streams that have the container label `loki` but are not in the namespace `prod` will have a `72h` retention period.
   156  - For tenant `30`:
   157    - All streams except those having the container label `nginx` will have the global retention period of `744h`, since there is no override specified.
   158    - Streams that have the label `nginx` will have a retention period of `24h`.
   159  
   160  ## Table Manager
   161  
   162  In order to enable the retention support, the Table Manager needs to be
   163  configured to enable deletions and a retention period. Please refer to the
   164  [`table_manager`](../../../configuration#table_manager)
   165  section of the Loki configuration reference for all available options.
   166  Alternatively, the `table-manager.retention-period` and
   167  `table-manager.retention-deletes-enabled` command line flags can be used. The
   168  provided retention period needs to be a duration represented as a string that
   169  can be parsed using the Prometheus common model [ParseDuration](https://pkg.go.dev/github.com/prometheus/common/model#ParseDuration). Examples: `7d`, `1w`, `168h`.
   170  
   171  > **WARNING**: The retention period must be a multiple of the index and chunks table
   172  `period`, configured in the [`period_config`](../../../configuration#period_config)
   173  block. See the [Table Manager](../table-manager#retention) documentation for
   174  more information.
   175  
   176  > **NOTE**: To avoid querying of data beyond the retention period,
   177  `max_look_back_period` config in [`chunk_store_config`](../../../configuration#chunk_store_config) must be set to a value less than or equal to
   178  what is set in `table_manager.retention_period`.
   179  
   180  When using S3 or GCS, the bucket storing the chunks needs to have the expiry
   181  policy set correctly. For more details check
   182  [S3's documentation](https://docs.aws.amazon.com/AmazonS3/latest/dev/object-lifecycle-mgmt.html)
   183  or
   184  [GCS's documentation](https://cloud.google.com/storage/docs/managing-lifecycles).
   185  
   186  Currently, the retention policy can only be set globally. A per-tenant retention
   187  policy with an API to delete ingested logs is still under development.
   188  
   189  Since a design goal of Loki is to make storing logs cheap, a volume-based
   190  deletion API is deprioritized. Until this feature is released, if you suddenly
   191  must delete ingested logs, you can delete old chunks in your object store. Note,
   192  however, that this only deletes the log content and keeps the label index
   193  intact; you will still be able to see related labels but will be unable to
   194  retrieve the deleted log content.
   195  
   196  For further details on the Table Manager internals, refer to the
   197  [Table Manager](../table-manager/) documentation.
   198  
   199  
   200  ## Example Configuration
   201  
   202  Example configuration with GCS with a 28 day retention:
   203  
   204  ```yaml
   205  schema_config:
   206    configs:
   207    - from: 2018-04-15
   208      store: bigtable
   209      object_store: gcs
   210      schema: v11
   211      index:
   212        prefix: loki_index_
   213        period: 168h
   214  
   215  storage_config:
   216    bigtable:
   217      instance: BIGTABLE_INSTANCE
   218      project: BIGTABLE_PROJECT
   219    gcs:
   220      bucket_name: GCS_BUCKET_NAME
   221  
   222  chunk_store_config:
   223    max_look_back_period: 672h
   224  
   225  table_manager:
   226    retention_deletes_enabled: true
   227    retention_period: 672h
   228  ```