NeuroAgent

How to Configure Auto-Deletion of Logs in Grafana/Loki: Complete Guide

Complete guide to configuring auto-deletion of logs in Loki with filesystem storage. Step-by-step instructions with configuration examples and solutions to common problems.

Question

How to properly configure auto-deletion of logs in Grafana/Loki?

I’m trying to enable auto-deletion in Grafana/Loki, but the options specified in the official documentation (retention., delete.) are not working. My system is deployed using Docker, and the storage is configured as filesystem.

Here is my current 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

What are the correct settings for auto-deletion to automatically remove old logs? What settings in the configuration need to be changed or added?

NeuroAgent

Your configuration contains several key errors that prevent auto-deletion of logs in Loki with filesystem storage. The main issues are related to incorrect use of table_manager (deprecated component), insufficient retention settings, and incorrect compactor configuration.

Table of Contents

Main problems in the current configuration

Your current configuration contains several critical errors:

  1. Using table_manager - this component is deprecated and no longer recommended for managing retention. According to the Loki documentation, table_manager has been replaced by compactor.

  2. Too short intervals - you set retention_period: 10s and compaction_interval: 10s, which doesn’t give the system enough time to process data.

  3. Incorrect schema_config structure - to work with retention, you need to use tsdb or boltdb-shipper storage engine.

Correct configuration for filesystem storage

For proper auto-deletion with filesystem storage, the following is required:

yaml
schema_config:
  configs:
    - from: 2020-10-24
      store: tsdb
      object_store: filesystem
      schema: v13
      index:
        prefix: index_
        period: 24h

limits_config:
  retention_period: 168h  # 7 days, must be in hours
  # ... other settings

compactor:
  working_directory: /tmp/loki/compactor
  compaction_interval: 1h
  retention_enabled: true
  retention_delete_delay: 2h
  delete_request_store: filesystem
  retention_delete_worker_count: 150

Required parameters for auto-deletion

To enable auto-deletion in Loki with filesystem storage, the following parameters are required:

In the limits_config section:

  • retention_period: retention period in hours (e.g., 168h for 7 days)
  • This parameter determines how long logs should be kept before deletion

In the compactor section:

  • retention_enabled: true - enables the old log deletion function
  • delete_request_store: filesystem - specifies where to store deletion requests
  • working_directory - working directory for the compactor
  • compaction_interval - compaction interval (should be greater than retention_period)
  • retention_delete_delay - delay before deletion (recommended 2 hours)

In the schema_config section:

  • store: tsdb or store: boltdb-shipper - required storage engines for retention to work
  • object_store: filesystem - indicates the use of the file system

Step-by-step setup

1. Remove table_manager

Completely remove the table_manager section as this component is deprecated:

yaml
# DELETE THIS SECTION
table_manager:
  retention_deletes_enabled: true
  retention_period: 740h

2. Configure limits_config

Change retention_period to a reasonable value in hours:

yaml
limits_config:
  retention_period: 168h  # 7 days
  # ... other settings

3. Properly configure compactor

Add all necessary parameters for compactor:

yaml
compactor:
  working_directory: /tmp/loki/compactor
  compaction_interval: 1h
  retention_enabled: true
  retention_delete_delay: 2h
  delete_request_store: filesystem
  retention_delete_worker_count: 150

4. Check schema_config

Ensure the correct storage engine is being used:

yaml
schema_config:
  configs:
    - from: 2020-10-24
      store: tsdb  # or boltdb-shipper
      object_store: filesystem
      schema: v13
      index:
        prefix: index_
        period: 24h

5. Update docker-compose

Ensure all necessary directories are mounted:

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
      - ./loki-compactor:/tmp/loki/compactor  # Add this line
    ports:
      - "3100:3100"

Verification of auto-deletion

After applying the correct configuration, you can verify that auto-deletion is working:

  1. Check compactor logs:

    bash
    docker logs loki | grep -i "compactor\|retention\|delete"
    
  2. Check compactor directory:

    bash
    ls -la /tmp/loki/compactor/
    
  3. Check chunks directory:

    bash
    ls -la /tmp/loki/chunks/
    
  4. Use Loki API to check:

    bash
    curl "http://localhost:3100/loki/api/v1/status/buildinfo"
    

Common errors and solutions

Error 1: Compactor doesn’t start

Problem: Compactor doesn’t start due to missing required directories.
Solution: Ensure that the working_directory exists and has correct permissions:

bash
mkdir -p /tmp/loki/compactor
chmod 755 /tmp/loki/compactor

Error 2: Data isn’t being deleted

Problem: Logs aren’t being deleted despite correct configuration.
Reason: As mentioned in the StackOverflow answer, Loki won’t delete old storage chunks. Instead, configure TTLs in the chunk store (for S3/GCS) or use lifecycle policies.

Error 3: Too short intervals

Problem: compaction_interval is too short.
Solution: Increase compaction_interval to a reasonable value (1-24 hours) and ensure it’s greater than retention_period.

Error 4: Incorrect storage engine

Problem: Using unsupported storage engine.
Solution: Ensure that schema_config uses store: tsdb or store: boltdb-shipper.

Sources

  1. Log retention | Grafana Loki documentation
  2. Log entry deletion | Grafana Loki documentation
  3. Why my Loki log retention is not working? - Stack Overflow
  4. Enabling Log Retention in Loki with Compactor
  5. Managing Grafana Loki Log Retention and Deletion Delay in Docker

Conclusion

To properly configure auto-deletion of logs in Loki with filesystem storage, you need to:

  1. Use tsdb or boltdb-shipper storage engine in schema_config
  2. Configure compactor with retention_enabled: true and correct paths
  3. Remove the deprecated table_manager
  4. Set reasonable retention_period values in hours
  5. Configure compaction_interval greater than retention_period
  6. Ensure access to working directories with correct permissions

After making these changes, the system will automatically delete old logs according to the specified retention period.