Skip to main content
Cloud DevOps Solutions
Get a quote Talk to an engineer

Logging & Tracing

Log Retention and Index Lifecycle Management Without the Storage Bill Shock

Retention decides how long each kind of log is kept and where. With Elasticsearch we use data streams and index lifecycle management to roll over, move data from hot to warm and cold storage, snapshot it, and delete it on schedule. With Loki we set retention per stream and use bucket lifecycle rules.

Key takeaways

  • Retention should come from requirements (incidents, audits, law), not from whatever the default disk size allows.
  • ILM automates rollover, tier movement and deletion so nobody clears disks by hand.
  • Shard size and count matter more to cluster health than raw data volume.
  • Different log classes get different retention, and compliance logs are protected from cost-driven deletion.
  • Snapshots and object storage lifecycle rules provide cheap long-term history.

The slow way logging bills grow

Log storage rarely breaks on a single day. It grows quietly. Daily indices pile up, nobody remembers why a particular index is kept, disks creep toward their limits, and eventually a watermark triggers and the cluster stops accepting writes during a busy period. The usual response is an emergency delete and a vow to automate it later.

Retention work is that automation, done deliberately. It is a core part of our logging, ELK, Loki and OpenTelemetry service and works hand in hand with log routing by storage tier.

What good looks like

  • Retention is decided per log class and signed off by the people who rely on it.
  • Data moves through hot, warm, cold and delete phases automatically.
  • Shards are a healthy size and the shard count per node is bounded.
  • Long-term history sits in cheap object storage and has been restored at least once in testing.
  • Disk and shard metrics are alerted well before a limit is hit.

Start with requirements, not tooling

We ask four questions per log class:

  1. How far back do engineers actually search during incidents and post-mortems?
  2. What do auditors, customers or regulators require, and for how long?
  3. Is there a legal obligation to delete after a period, such as personal data limits?
  4. How fast must old data come back when someone asks for it?

Answers differ. Application debug logs may need two weeks. Authentication events may need a year. A privacy requirement can push the other way, requiring deletion on a timer, which links to our work on log scrubbing for GDPR and HIPAA. Retention is therefore a ceiling as well as a floor.

How it works in Elasticsearch

Data streams and rollover

Time-series logs belong in data streams. A data stream writes to a backing index and rolls over when conditions are met, so you never need date-named indices managed by scripts. An ILM policy attached through an index template controls the lifecycle.

PUT _ilm/policy/app-logs
{
  "policy": {
    "phases": {
      "hot": {
        "actions": {
          "rollover": { "max_primary_shard_size": "50gb", "max_age": "1d" }
        }
      },
      "warm": {
        "min_age": "7d",
        "actions": {
          "forcemerge": { "max_num_segments": 1 },
          "set_priority": { "priority": 50 }
        }
      },
      "cold": {
        "min_age": "30d",
        "actions": { "set_priority": { "priority": 0 } }
      },
      "delete": {
        "min_age": "90d",
        "actions": { "delete": {} }
      }
    }
  }
}

The ages and sizes are examples. Rollover size is chosen from a target shard size appropriate to your hardware and query behaviour. Tier movement relies on node roles and data tier preferences being set correctly, which we configure with the cluster, as in our platform deployment page.

Snapshots and searchable snapshots

Snapshot lifecycle management writes regular snapshots to object storage. They serve two purposes: disaster recovery and cheap long-term retention. Where your licence and version allow it, searchable snapshots can make cold or frozen data queryable directly from object storage at lower cost, trading some query speed. Check the feature and licence tier for the version you run before designing around it.

Cleaning up old data

For clusters that already have daily indices, we create a migration plan rather than a big bang. Existing indices can be attached to policies, or reindexed into data streams during quiet hours with throttling. Each step is validated with document counts.

How it works in Loki

Loki is simpler. Chunks live in object storage and the compactor applies retention. A global period is set in limits_config, and per-stream overrides use selectors:

limits_config:
  retention_period: 336h
  retention_stream:
    - selector: '{namespace="kube-system"}'
      priority: 1
      period: 72h
    - selector: '{log_class="audit"}'
      priority: 2
      period: 8760h
compactor:
  working_directory: /loki/compactor
  retention_enabled: true
  delete_request_store: s3

Check the exact keys against your Loki version because configuration has evolved. Then align the bucket lifecycle rules so they do not delete objects sooner than Loki expects. Mismatch between the two layers is a classic source of “missing logs”.

Decisions and trade-offs

Keep everything or sample. Keeping all data is simplest and sometimes legally needed. For non-critical logs, shorter retention is the largest cost lever you have.

Warm and cold tiers: worth it? They save money when volume is large and old data is rarely queried. For small clusters the extra node roles add complexity for little benefit, and one tier with a delete phase is enough.

Self-managed snapshots versus managed backup. Snapshots are easy to configure and easy to forget to test. We always test a restore, as with the practices on our backup and disaster recovery page.

When not to over-engineer. A team shipping a few gigabytes a day with 14 days of retention needs one policy and an alert on disk usage, not a five-phase lifecycle.

Common failure modes

  • Policy attached but never applied because the template did not match new indices. We verify with the ILM explain API.
  • Rollover stuck because of a missing alias or a read-only block caused by disk watermarks.
  • Over-sharded indices from daily indices on low-volume services. Data streams with size-based rollover avoid this.
  • Deletes that violate holds. Compliance streams are tagged and excluded from cost-driven deletion.
  • Unrestorable snapshots, found only during an emergency. Scheduled restore tests catch this.

Reviewing retention as the system changes

A retention matrix is correct on the day it is signed off and drifts afterwards. New services appear, a regulator changes expectations, or a product starts logging more per request. We recommend a short review at a regular interval, using three questions. Which indices or streams grew fastest? Did anyone actually query data from the older tiers? Did any restore test fail or take longer than planned?

The answers drive concrete changes: moving a stream to a shorter hot phase, splitting a noisy application into its own policy so it does not inflate the shared one, or lengthening retention for a source that auditors keep asking about. Because the policies are code, each change is a reviewed pull request with a stated reason.

The goal is that nobody has to guess why a particular number of days was chosen.

Delivery and handover

You receive the retention matrix signed off by your stakeholders, the policies and templates as code, alerts for disk, shards and ILM errors, and a runbook for changing retention and restoring data. We run a restore drill with your team so the procedure is familiar.

How to tell it is working

Disk usage follows a predictable curve, shard counts stay in bounds, ILM errors are zero, and a restore from last month’s snapshot succeeds when tested. If your cluster is creeping toward its limits, contact us.

References

How we deliver it

  1. 01

    Gather requirements

    We ask how far back engineers actually search, what auditors and regulators require, and what legal hold means for you.

  2. 02

    Design the lifecycle

    Phases, ages, sizes and storage classes are written down per log class, with cost estimates.

  3. 03

    Implement as code

    Index templates, data streams and ILM policies are applied through Terraform, the API or operator manifests.

  4. 04

    Migrate existing data

    Old time-based indices are brought under policy or reindexed in a controlled, throttled way.

  5. 05

    Add snapshots and restore tests

    Snapshots go to object storage, and we prove a restore works before relying on it.

  6. 06

    Monitor and revisit

    Shard counts, disk watermarks and policy errors are alerted, and retention is reviewed against real usage.

Tools and technologies

Elasticsearch ILMData streamsIndex templatesSnapshot lifecycle managementSearchable snapshotsKibanaGrafana LokiAmazon S3 lifecycle rulesTerraformCurator

Frequently asked questions

How long should we keep logs?
It depends on purpose. Debugging logs are often useful for days to a few weeks. Security and audit logs may need months or years depending on your regulations and contracts. We set retention per log class from your requirements, then balance it against cost, rather than choosing one number for everything.
What is Elasticsearch ILM?
Index lifecycle management is an Elasticsearch feature that automates what happens to an index over time. A policy defines phases such as hot, warm, cold and delete, with actions like rollover, shrink, force merge and tier migration. It replaces manual scripts and keeps storage predictable.
Why does shard size matter?
Each shard carries memory and management overhead in the cluster. Thousands of tiny shards slow the cluster and waste heap, while huge shards are slow to recover. Rolling over by size and age keeps shards in a healthy range, and data streams make that pattern the default.
How does retention work in Loki?
Loki applies retention through its compactor, using a global period or per-stream rules, and your object storage can also have lifecycle policies. Because chunks live in object storage, long retention is cheap compared with indexed storage. Take care that both layers agree, or data may be kept or deleted unexpectedly.
Can we keep logs cheaply for years?
Yes. Compressed copies in object storage with archive storage classes are inexpensive, and Elasticsearch searchable snapshots can make cold data queryable at lower cost. Retrieval from deep archive can be slow or carry fees, so we plan which logs need fast access and which just need to exist.

Talk to an engineer about elasticsearch index lifecycle management

Book a free 30-minute call. We review your current setup and tell you honestly whether and how we can help.

Written by Saddique Khan, DevOps / SRE / Cloud Consultant Engineer. Last updated 2026-10-08.