Delete without losing history
Deleted rows must be recoverable, audited, or filtered out of caches already fanned out.
Say it like this
Deletes set a deleted_at tombstone instead of removing the row, so the feed and chat caches that already fanned the id out can filter it at read time, and support can restore it inside the retention window.
Basic solution
Set a deleted_at tombstone instead of removing the row; filter it on read and purge later.
Tombstone on write, filter on read, purge later
The delete is one UPDATE. Timelines keep the id; hydration drops it when the body is missing or tombstoned. A purge job hard-deletes after the retention window.
How it works
- A delete request does not remove the row. Instead it sets deleted_at = now (and optionally deleted_by) on the row, in the same transaction as any related updates. The row keeps all its data.
- Every read path adds the filter WHERE deleted_at IS NULL, either written into each query or applied automatically through a database view or an ORM default scope, so tombstoned rows are invisible to normal reads.
- Derived copies of the row (caches, the search index, the ids already fanned out into timelines) are handled one of two ways: evicted at delete time, or left in place and dropped during hydration when the reader fetches the row and sees the tombstone. The second is more common, because the copies can number in the millions.
- A restore clears deleted_at and the row is live again. For this to work, unique indexes must ignore tombstoned rows, so they are partial: UNIQUE (email) WHERE deleted_at IS NULL.
- A purge job hard-deletes rows whose deleted_at is older than the retention window, and deletes the blobs and index entries that belong to them.
Why tombstone rather than delete
- A delete that has already been fanned out to a million timelines cannot be chased down synchronously; a tombstone lets every reader discover it lazily.
- Undo, audit and abuse investigation all need the row to still exist for a while.
Worked example
News feed post deletion
- A post by a user with 2M followers was pushed into 2M timeline caches (ZADD per follower) at write time.
- Hard delete would require 2M ZREM operations before the post stops appearing; at 50K ops/s that is 40 seconds during which readers still see it, and any failure leaves stragglers.
- Soft delete: one UPDATE posts SET deleted_at = now WHERE id = X, plus one cache eviction of the post body. Cost: 2 operations.
- Readers: the feed service fetches 800 ids from the timeline cache, hydrates bodies with MGET, and drops any whose body is missing or carries deleted_at. The post disappears from every feed on the next refresh, within seconds.
- Cleanup: a nightly job trims deleted ids from timelines it happens to touch, and a purge job hard-deletes posts 30 days after deleted_at. Deleted rows are under 1% of the table, so the extra storage is noise.
Chat message "delete for everyone"
- Same shape: the message row gets deleted_at and the body is blanked; clients that already hold the message receive a tombstone event and replace it with "message deleted".
Variants and when each wins
deleted_at column on the same table
- One nullable timestamp column and one filter in every query. It wins as the default for simplicity, though every query and index must account for the column and the table carries dead rows until purge.
Archive table
- The row is moved to a separate posts_deleted table on delete. It wins when the live table is hot and large, because it stays clean and its indexes stay small; restores and joins against archived data are more work.
Status enum
- A state column (active, hidden, deleted, banned) where deletion is one visibility state among several. It wins when moderation already needs several states, though it is easy to forget a state in a filter.
Event-sourced tombstone
- A Deleted event is appended to the log and every projection applies it. It wins when the system is already event-sourced; otherwise it is overkill.
Use deleted_at by default, an archive table when the live table is hot and large, and a status enum when moderation already needs several states.
Failure modes
Forgotten filter
- Deleted rows show up in one report or endpoint. It happens because someone wrote a query without WHERE deleted_at IS NULL. Apply the filter in one place queries cannot bypass, a view or an ORM default scope, and add a test that inserts a tombstoned row and asserts each endpoint hides it.
Unique constraint blocks re-creation
- A user who deleted their account cannot sign up again with the same email. It happens because the plain UNIQUE index still sees the tombstoned row. Use a partial unique index that applies only WHERE deleted_at IS NULL, or move deleted rows to an archive table.
Derived copies keep serving
- The post is gone from the page but still appears in search results and push notifications. It happens because the search index and the notification queues were never told about the delete. Publish a deleted event through CDC or the outbox so every derived store applies the tombstone.
Table bloat
- The table and its indexes keep growing even though the number of live rows does not. It happens because there is no purge job, so tombstoned rows accumulate forever. Schedule a hard delete after the retention window, in batches, cascading to blobs.
Restore resurrects stale state
- A restored post reappears with old counters or in the wrong position. It happens because its derived data was purged while the row was tombstoned. Treat a restore as a re-create event that rebuilds every derived copy.
Interviewer follow-ups
Answer out loud before you open each one.
Why not just delete the row and remove it from every cache?
Because the fan-out is one-to-many and asynchronous: a popular post lives in millions of timelines, and chasing all of them synchronously is slow and fragile. A tombstone makes every reader discover the deletion on its next read, which is both cheaper and more reliable.
How do you stop deleted rows leaking into a new query someone writes next year?
Put the filter in one place the query cannot bypass: a view, a row-level security policy, or an ORM default scope, and add a regression test that seeds a tombstoned row and checks each public endpoint hides it.
What does GDPR erasure mean for a soft delete?
The tombstone is only the first step. Personal data must actually be removed within the legal window, so the purge job must run on schedule, cascade to backups by expiry, and blank or hash fields that cannot wait for the purge.
How is a soft delete different from a tombstone in Cassandra?
Same idea, different layer. Cassandra writes a tombstone marker for any delete and removes it at compaction after gc_grace; a soft delete is an application-level marker you control and can restore from. Both cost reads until they are purged.
Connections
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Amazon EC2 Auto Scaling | Post service, Feed service, Purge job | Compute Engine managed instance groups | Virtual Machine Scale Sets | Kubernetes Deployment with a HorizontalPodAutoscaler, Nomad |
| Amazon Aurora | Posts table | AlloyDB for PostgreSQL, Cloud SQL | Azure Database for PostgreSQL or MySQL | PostgreSQL, MySQL, CockroachDB |
| Amazon ElastiCache | Post cache, Timelines ×2M | Memorystore | Azure Managed Redis | Redis, Valkey, Memcached |