How to implement soft deletes
· Category: SQL & Databases
Short answer
Soft deletes mark rows as deleted without removing them from the table, typically using a deleted_at timestamp or is_active boolean.
Steps
- Add a
deleted_atTIMESTAMP NULLABLE column to the table. - On delete, run
UPDATE users SET deleted_at = NOW() WHERE id = 1;instead ofDELETE. - Filter active rows in queries:
SELECT * FROM users WHERE deleted_at IS NULL; - Use a database view or application scope to apply the filter automatically.
- Provide an admin interface to restore or permanently purge soft-deleted rows.
Tips
- Add an index on
deleted_atto keep active-row queries fast. - Use partial indexes in PostgreSQL to exclude soft-deleted rows entirely.
Common issues
- Forgetting to add
WHERE deleted_at IS NULLreturns deleted data accidentally. - Unique constraints may conflict with soft-deleted rows unless the index is partial.r