Skip to content
Navigation

Type at least two characters. Search covers page titles, headings, tags and database names.

↑ ↓ to navigateEnter to openEsc to close0 pages
Production Best Practicesintermediate

Data Retention

Expiring data by design rather than by emergency deletion, and moving cold data out of the hot path.

2 min readIntermediateUpdated Edit this page

Retention is a design decision made at table creation, not a cleanup job invented when the disk fills. The mechanism you choose determines whether expiry is free or expensive.

Delete is the expensive option

A bulk DELETE is a write-heavy operation: it produces dead tuples in PostgreSQL, undo records and secondary index changes in InnoDB, tombstones in Cassandra, and part rewrites in ClickHouse. It generates replication traffic proportional to the rows removed, and it does not return space to the filesystem in most engines.

Drop a partition instead

If a table has a time-based lifecycle, partition it by time and expire by dropping partitions. Dropping is a metadata operation: it is instant, frees space immediately, produces almost no WAL, and generates no dead rows.

This one decision changes retention from an ongoing operational burden into a scheduled no-op. See Partitioning.

Engine-native equivalents:

  • ClickHouseTTL clauses drop or move parts automatically. See TTL.
  • Cassandra — per-row or per-column TTL, though expired data still becomes tombstones.
  • Redis — key expiry with EXPIRE; memory is reclaimed lazily and by the active expiry cycle.
  • Elasticsearch / OpenSearch — index lifecycle management deletes whole indices by age. See Index Lifecycle Management.
  • MongoDB — TTL indexes remove documents in a background pass.

Data Archiving

Archiving moves data out of the operational store while keeping it available. It is worth doing when data must be retained for years but is queried rarely.

A workable pattern:

  1. Export the partition to columnar files (Parquet) in object storage, partitioned by the same time key.
  2. Verify the export — row counts and a checksum against the source.
  3. Detach and drop the partition.
  4. Query the archive with a separate engine when it is needed.

The result is a small, fast operational database and cheap long-term storage, instead of a database that is 95% cold data raising the cost of every backup, restore and index rebuild.