Move old data to cheaper storage, or let it expire automatically, so the main database stays small.
Data archiving moves old data you rarely need out of your main database into cheaper storage. TTL (time to live) makes data expire and disappear by itself after a set time.
Most data is useful for a short time. Last week's orders are read all the time. Orders from five years ago are read almost never. If you keep everything in the main database, it grows without limit, and everything about it gets slower and costs more.
There are three common answers. Archive the old data to cheaper storage (for example files in object storage). Expire it automatically with a TTL when a rule says it is no longer needed. Or delete it in a planned job. Many systems combine them: keep 90 days in the database, archive up to 7 years, then delete.
TTL is a setting on a row, key or document that says when it expires. Redis, DynamoDB, Cassandra, MongoDB and most time-series databases have a version of it. Expiry is usually done in the background, so data can stay a little longer than its TTL before it is physically gone.
| Method | How it works | Good and bad |
|---|---|---|
| Row-by-row DELETE | A job deletes rows older than the limit | Easy to write. Heavy: locks, big logs, empty space left behind, and slow on huge tables. |
| Drop a time partition | The table is split by day or month, and a whole old partition is dropped or detached | Fast and light. Needs the table to be partitioned by time from the start. See Partitioning. |
| TTL / expiry | Each row, key or document carries an expiry time and the database removes it | No job to write. Removal happens in the background, so it is not instant. |
| Archive then delete | Copy old rows to cheap storage (such as object storage), check them, then remove them | Keeps the data for audits. Costs a copy step, and the archive must be searchable and restorable. |
| Soft delete | Mark the row as deleted but keep it | Easy to undo. The data is still there, so the table does not shrink and privacy rules may not be met. |
Every night a job deletes rows older than the retention. Deleting row by row is real work: it writes the log, holds locks and leaves empty space behind.
A simplified model with exact arithmetic (no random numbers). The query time (5 ms plus 3 ms per million rows), the delete speed (2 M rows a minute), the 1 second partition drop and the copy speed are made-up numbers for teaching, not measurements. Real databases behave differently, and a good index keeps many queries fast on a big table.
| Term | Meaning in easy words |
|---|---|
| Retention | How long a kind of data is kept |
| TTL (time to live) | A per-row, per-key or per-document expiry time after which the data is removed |
| Archive | Cheaper storage for data that is kept but rarely read |
| Hot data | Recent data that is read and written often |
| Cold data | Old data that is rarely read |
| Partition drop | Removing a whole partition, which is a quick metadata change |
| Soft delete | Marking a row as deleted without removing it |
| Hard delete | Physically removing the data |
| Legal hold | An order that stops deletion of certain data |
| Lazy expiry | Removing expired data when it is next read, or in the background, not at the exact moment it expires |
| Compaction / vacuum | Cleanup that frees space left behind by deletes |
| Choice | We gain | We lose |
|---|---|---|
| Short retention in the database | Small, fast, cheap | Old data is slower or harder to reach |
| Long retention in the database | Everything is one query away | Bigger tables, slower, costlier |
| Partition by time | Instant removal of old data | Planning up front, and queries across partitions need care |
| Row-by-row DELETE | Works on any table | Heavy load and locks, slow on big tables |
| TTL | No cleanup job, automatic | Not instant, and a wrong TTL silently drops data |
| Soft delete | Easy undo, an audit trail | The table keeps growing, and privacy rules may not be met |