How to Implement Soft Deletes with Partial Indexes
Implement soft deletes with a tombstone column and partial indexes so deleted rows leave the hot index but remain recoverable and auditable.
On this page
Introduction
Physical deletion is irreversible and can break referential integrity and reporting. Many applications instead mark a row as deleted with a tombstone and hide it from normal queries. This soft delete preserves history, allows recovery, and keeps foreign keys intact, at the cost of a filter on every read and indexes that otherwise grow to include the deleted rows. Partial indexes make soft delete practical by keeping the hot index proportional to the live data.
This tutorial implements soft deletes with a deleted_at tombstone, a mandatory query scope, and partial indexes on live rows, plus a unique constraint that still allows a name to be reused after deletion. It ends with tests that prove deleted rows are hidden from normal reads but recoverable.
Add a tombstone column
Represent deletion as a nullable timestamp, deleted_at, which is NULL for live rows and set to the deletion instant otherwise. A timestamp records when the deletion happened, which a boolean flag does not and which audits usually need. Keep the row in place so its foreign keys and child rows remain valid, and so a mistaken deletion can be undone by clearing the column.
Soft delete is not a substitute for retention. The rows accumulate, so pair it with a retention policy that eventually hard-deletes or archives the oldest tombstoned rows. Otherwise the table grows without bound even though users believe the data is gone. Decide the retention period for deleted rows and enforce it so soft delete does not become permanent storage.
ALTER TABLE documents ADD COLUMN deleted_at timestamptz;
CREATE INDEX idx_documents_deleted_at ON documents (deleted_at) WHERE deleted_at IS NOT NULL;
-- soft delete and restore
UPDATE documents SET deleted_at = now() WHERE id = $1;
UPDATE documents SET deleted_at = NULL WHERE id = $1;
Scope every read and enforce it
Soft delete only works if every read filters deleted_at IS NULL. A single query that forgets the filter shows deleted rows, and a single update can modify a deleted row. Make the filter structural: a view that exposes only live rows, a data-access layer that appends the filter automatically, or a row-level policy that hides tombstoned rows except from an admin role. The goal is that the default is live-only and seeing deleted rows requires an explicit, named action.
For a soft-delete-aware unique constraint, make the uniqueness apply only to live rows using a partial unique index on the business key WHERE deleted_at IS NULL. Then a deleted ‘default’ document does not block creating a new live ‘default’, which is exactly the behavior users expect. A plain unique constraint would reject the second one permanently because the deleted row still exists.
-- uniqueness only among live rows
CREATE UNIQUE INDEX uq_documents_live_slug
ON documents (slug)
WHERE deleted_at IS NULL;
-- a view so readers cannot accidentally see tombstoned rows
CREATE VIEW live_documents AS
SELECT * FROM documents WHERE deleted_at IS NULL;
Index only the live rows
The hot queries want live rows, so index them with a partial index WHERE deleted_at IS NULL. This index excludes the growing set of deleted rows, so it stays proportional to the active data and does not slow as deletions accumulate. Without it, a full index includes every tombstoned row and grows forever, so the read that should touch a small slice traverses a larger structure.
Keep a separate small index on deleted_at for the admin and retention paths that do want deleted rows, such as listing a user’s deleted documents or finding rows to purge. That index is partial on deleted_at IS NOT NULL so it also excludes the live majority. Two small, targeted indexes beat one large index that serves neither path well.
-- hot path: live rows only
CREATE INDEX idx_documents_live_created
ON documents (owner_id, created_at DESC)
WHERE deleted_at IS NULL;
-- admin path: find deleted rows, e.g. for retention
CREATE INDEX idx_documents_deleted
ON documents (deleted_at)
WHERE deleted_at IS NOT NULL;
Flow deletions through one path
Centralize the delete operation. A single function that sets deleted_at, records who deleted it, and cascades to dependent rows keeps the behavior consistent. If application code sets the column ad hoc, some paths will forget a dependent row or a required audit entry. Keep the deletion logic in one place and call it, rather than updating the column from many call sites.
Decide how soft delete interacts with dependent rows. A parent deleted softly may need its children hidden too, either by cascading the tombstone or by making the child’s read scope check the parent. Referential integrity is preserved because the rows still exist, but the application must ensure children of a deleted parent are not shown as live. Encode that rule in the read scope, not in each query.
Test visibility, recovery, and index shape
Assert a deleted row is hidden from the normal read and from the live view. Assert an admin path can still find it. Assert recovery clears the column and the row reappears. Assert that recreating a business key with the same value as a deleted row succeeds, proving the partial unique index allows it. Assert the partial index excludes the deleted row by checking that a query filtering deleted_at IS NULL uses the live index in its plan.
psql -d app <<'SQL'
UPDATE documents SET deleted_at = now() WHERE id = 1;
SELECT count(*) FROM live_documents WHERE id = 1; -- expect 0
SELECT count(*) FROM documents WHERE id = 1; -- expect 1 (still present)
INSERT INTO documents (slug) VALUES ('default'); -- succeeds: live unique only
SQL
Common pitfalls
Forgetting the deleted_at IS NULL filter on a read, using a full index instead of a partial one so performance degrades as deletions grow, a plain unique constraint that blocks reusing a deleted key, leaving tombstoned rows forever so the table grows unbounded, and cascading deletes inconsistently across dependent rows are the recurring mistakes. A structural read scope, partial indexes, a partial unique index, retention, and centralized deletion address them.
Conclusion
Soft delete with a tombstone keeps history and recovery while hiding rows from normal use. Add deleted_at, scope every read structurally, index only live rows with a partial index, allow key reuse with a partial unique index, and plan retention for the tombstones. Self-check by asserting a deleted row is hidden from the live view but present and recoverable, and that its key can be reused. Then deletion is reversible without making the hot path slower.
Sources: PostgreSQL: Partial indexes, PostgreSQL: NULLS and indexes, PostgreSQL: Unique partial indexes.
FAQ
- Soft delete or hard delete?
- Soft delete when you need recovery, an audit trail, or referential integrity to other rows. Hard delete when a strict erasure requirement applies or the rows are large and truly disposable. The tombstone column keeps the row but hides it from normal queries.
- Why do partial indexes matter for soft deletes?
- The hot queries filter deleted_at IS NULL, so most reads want only live rows. A partial index on live rows is far smaller than a full index and stays proportional to active data, while deleted rows accumulate outside it. This keeps performance stable as deleted history grows.
Report a problem with this articleOpens a pre-filled issue on GitHub.