Soft Delete

Difficulty: Beginner

Question

What is a soft delete, how do you implement it correctly, and what problems does it introduce compared to a hard delete?

Answer

A soft delete marks a row as deleted, usually by setting a deleted_at timestamp or an is_deleted flag, instead of physically removing it with DELETE. Queries then filter out marked rows so the application behaves as if the data is gone. It makes recovery, auditing and undo easy, but every query must remember the filter, unique constraints need special handling, and the data is still stored, which matters for privacy laws.

Typical implementation

  • Add a nullable deleted_at TIMESTAMPTZ column. NULL means active; a timestamp means deleted and records when. This is more useful than a boolean because you know when it happened and can purge old rows.
  • Replace DELETE FROM users WHERE id = 7 with UPDATE users SET deleted_at = now() WHERE id = 7.
  • Add WHERE deleted_at IS NULL to every read, ideally centrally:
  • a database view such as active_users,
  • an ORM global filter: Laravel's SoftDeletes trait, Hibernate's @SQLDelete with @SQLRestriction (which replaced @Where in Hibernate 6.3), or a Prisma client extension,
  • or PostgreSQL row-level security for stricter enforcement.

Why teams use it

  • Undo and recovery: "Restore deleted item" in a UI, or recovering from an accidental bulk delete.
  • Audit and disputes: keeping a cancelled order or deleted bank beneficiary for investigation.
  • Referential safety: child rows such as invoices still point to a valid parent.
  • Sync: mobile or offline clients need to learn that a record was deleted, which a missing row cannot tell them.

Problems it introduces

  • Forgotten filters. One report or admin query without deleted_at IS NULL shows deleted data. This is the most common bug.
  • Unique constraints break. A user deletes their account and signs up again with the same email; a plain UNIQUE(email) rejects it. Fix with a partial unique index: CREATE UNIQUE INDEX ... ON users(email) WHERE deleted_at IS NULL in PostgreSQL. MySQL lacks partial indexes, so teams use a generated column or include deleted_at in the key with care about NULL handling.
  • Foreign keys do not cascade. ON DELETE CASCADE never fires because nothing is deleted. You must soft delete children yourself or accept orphaned-looking data.
  • Table and index bloat. Dead rows stay in every index and slow scans over time.
  • Privacy and compliance. If a user exercises their right to erasure under GDPR or India's DPDP Act, a soft delete alone is usually not enough. You need to hard delete or anonymise personal fields.

Alternatives and hybrids

ApproachHow it worksGood for
Soft delete flagdeleted_at on the same tableUndo, short retention
Archive tableMove row to users_deleted in a transaction, then hard deleteKeeping live tables clean
Audit / history tableTrigger or CDC copies every changeFull history and compliance
Hard delete plus backupsPhysical delete, restore from backup if neededSimple data with no undo

A common production pattern: soft delete immediately, then a scheduled job hard deletes or anonymises rows older than a retention window such as 30 or 90 days.

Soft delete lifecycle. A row moves from active to hidden to permanently removed.
  1. Active row: deleted_at is NULL and the row appears in queries.
  2. User deletes: App sets deleted_at = now() instead of DELETE.
  3. Hidden from reads: Views or ORM filters exclude the row.
  4. Restore window: Setting deleted_at back to NULL undoes it.
  5. Purge job: After retention, the row is hard deleted or anonymised.

Code examples

Soft delete with a partial unique index in PostgreSQL

ALTER TABLE users ADD COLUMN deleted_at TIMESTAMPTZ;

-- Email must be unique only among active users
ALTER TABLE users DROP CONSTRAINT IF EXISTS users_email_key;
CREATE UNIQUE INDEX uq_users_email_active
  ON users (email) WHERE deleted_at IS NULL;

-- Soft delete
UPDATE users SET deleted_at = now() WHERE id = 7;

-- Central place for the filter
CREATE VIEW active_users AS
SELECT * FROM users WHERE deleted_at IS NULL;

-- Nightly purge after 90 days
DELETE FROM users WHERE deleted_at < now() - INTERVAL '90 days';

The partial index lets a deleted user's email be reused, the view centralises the filter, and the purge keeps the table from growing forever.

Key points

  • Soft delete sets deleted_at or is_deleted instead of removing the row
  • Prefer a deleted_at timestamp over a boolean for auditability and purging
  • Every read needs the filter; centralise it with views or ORM global filters
  • Use a partial unique index so deleted rows do not block reuse of unique values
  • Soft delete alone does not satisfy erasure requests under GDPR or the DPDP Act

Common follow-up questions

  • How would you handle foreign keys and cascading with soft deletes?
  • Soft delete vs archive table: which would you choose and why?
  • How would you implement soft delete globally in your ORM?
  • How do soft deletes affect index size and query performance over time?

Interview tips

  • Raise the unique constraint problem and partial index fix without being asked
  • Mention privacy and retention; it shows production thinking beyond the happy path
  • Describe the hybrid soft delete plus scheduled purge pattern as your recommended design

Concepts covered

Soft Delete, deleted_at, Partial Unique Index, Data Retention, Right to Erasure

More Database Design questions