Quick revision
The short answer
With a deleted_at column, every query must remember to filter deleted_at IS NULL, so we centralise the filter in views, Hibernate's @SQLRestriction (formerly @Where) or a repository base. A plain unique constraint on email blocks a deleted user's email from registering again, which a PostgreSQL partial unique index WHERE deleted_at IS NULL fixes. We also have to decide what foreign keys to soft-deleted rows mean, plan purging because the table never shrinks, and remember that soft delete is not erasure under privacy law.
What to remember
In this answer, we saw that soft deletes turn deletion into a convention the database does not know about. Queries must always filter deleted_at IS NULL, so we centralise it in views or @SQLRestriction; unique rules move to partial unique indexes so deleted rows do not block live ones; foreign keys still accept references to deleted rows, so we decide per relationship; and tables need a retention and purge plan, with real erasure or anonymisation for personal data.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
With a deleted_at column, every query must remember to filter deleted_at IS NULL, so we centralise the filter in views, Hibernate's @SQLRestriction (formerly @Where) or a repository base. A plain unique constraint on email blocks a deleted user's email from registering again, which a PostgreSQL partial unique index WHERE deleted_at IS NULL fixes. We also have to decide what foreign keys to soft-deleted rows mean, plan purging because the table never shrinks, and remember that soft delete is not erasure under privacy law.
Why this question is asked
Soft deletes are popular because they make 'undo' and audits easy: nothing is really gone. But the database does not know a row is 'deleted', so every rule that assumed deleted rows disappear now behaves differently. Interviewers ask this to see whether we know the common traps and their fixes before we add a deleted_at column to every table. In this answer, we will recap what soft delete means, look at the query leak, fix the unique constraint, decide what happens to references, and deal with growth and privacy.
1A Quick Recap of Soft Deletes
A hard delete removes the row with DELETE. A soft delete keeps the row and marks it as deleted, usually by setting a timestamp column such as deleted_at. A row with deleted_at IS NULL is live; any other value means it was deleted at that moment. The benefits are real: we can restore a row deleted by mistake, keep history for audits, and avoid breaking references from other tables.
The cost is that 'deleted' is now an application convention, not a database fact. Every query, constraint and relationship must be taught about it.
2Every Query Must Filter, So Do Not Rely on Memory
The most common bug is simply forgetting the filter in one query. A report, an export or an admin search then includes users who asked to leave.
We should put the filter in one place that queries go through by default. In the database that is a view, which most code reads instead of the table, so the filter is written once and cannot be forgotten in a new report.
3Centralising the Filter in Hibernate
In a JPA application, Hibernate's @SQLRestriction (called @Where before Hibernate 6.3) adds the condition to the SQL of every load of the entity. Spring Data users sometimes add it in a base repository instead. Notice that none of these protect native SQL written by hand, so reviews still need to watch for it.
4Unique Constraints Stop Working
Suppose users.email has a unique constraint. A user deletes their account, then signs up again months later with the same address. The deleted row still holds that email, so the insert fails, even though no live user has it.
A partial unique index only includes rows that match its WHERE clause, so uniqueness is enforced among live users only. Any number of deleted rows can share an email, but two live users cannot. This is the right rule for almost every natural key on a soft-deleted table. Case sensitivity is a separate decision; an index on lower(email) handles that.
5References to Soft-Deleted Rows
Foreign keys only know whether a row exists, and a soft-deleted row still exists. So orders referencing a deleted customer remain valid in the database's eyes, and nothing stops a new order being created for that customer. We have to decide, per relationship, what deletion means.
| Child | Typical decision | How |
|---|---|---|
| Orders of a deleted customer | Keep, history must stay | Leave as is, show 'deleted customer' |
| Sessions and tokens | Remove at once | Hard delete in the same transaction |
| Saved addresses | Soft delete with the parent | Set deleted_at in the same transaction |
| New orders after deletion | Refuse | Check in the service, or a trigger |
The last row is the subtle one. Because the foreign key still passes, preventing new references to a deleted row needs an explicit check in the service, or a composite foreign key to a unique (id, deleted_at) pair in more advanced designs. Writing these decisions down per entity avoids each developer guessing.
6Growth, Purging and Privacy
Soft-deleted rows never leave, so tables and indexes grow forever and queries read past ever more dead rows. Partial indexes (WHERE deleted_at IS NULL) help reads, but we should also plan a retention period: after, say, 90 days, a scheduled job moves soft-deleted rows to an archive table or hard deletes them in small batches.
And soft delete is not erasure. Under privacy laws such as the GDPR, a person can ask for their data to be erased, and a row with deleted_at set still holds their name, email and history. For personal data, soft delete can be a short grace period for undo, but it must be followed by a real delete or by anonymisation, overwriting personal fields while keeping the non-personal facts such as order amounts.
7Follow-up questions
How does MySQL handle the unique email case without partial indexes?
MySQL has no partial indexes, but a unique index allows any number of NULLs. So we add a generated column that holds the email only for live rows, for example active_email AS (IF(deleted_at IS NULL, email, NULL)), and put a unique index on it. MySQL 8.0.13 and later also accept a functional key part directly in the index definition. Live rows are then unique by email, and deleted rows all have NULL there and never conflict.
Why not use a boolean is_deleted instead of a timestamp?
A boolean works, but deleted_at carries more information for free: when the row was deleted, which drives retention and purge jobs. Some teams add deleted_by as well. The filter is just as cheap either way.
Does @SQLRestriction also hide rows from find by id?
Yes, the restriction is added to the SQL Hibernate generates for loading the entity, so a soft-deleted row is not found. Native queries are the exception, and associations pointing at a deleted row need a deliberate decision, because behaviour there depends on the mapping and version.
8Conclusion
In this answer, we saw that soft deletes turn deletion into a convention the database does not know about. Queries must always filter deleted_at IS NULL, so we centralise it in views or @SQLRestriction; unique rules move to partial unique indexes so deleted rows do not block live ones; foreign keys still accept references to deleted rows, so we decide per relationship; and tables need a retention and purge plan, with real erasure or anonymisation for personal data.
Open with
Soft delete means setting deleted_at instead of deleting, and the main problem is that the database still thinks the row is alive. First, every query has to filter deleted_at is null, and somebody always forgets once, so I put the filter in one place: a view that code reads by default, or Hibernate's @SQLRestriction on the entity, which was @Where before Hibernate 6.3.
Then, if they want more
Native SQL still needs care in reviews. Second, unique constraints. A unique constraint on email blocks a user who deleted their account from signing up again, because the deleted row still holds the email. In PostgreSQL I replace it with a partial unique index on email where deleted_at is null, so uniqueness applies only to live rows.
In MySQL, which has no partial indexes, I use a generated column that is the email for live rows and null otherwise, with a unique index, since nulls do not conflict.
Third, foreign keys still accept references to soft-deleted rows, so for each relationship I decide whether children stay, get soft deleted too, or are removed, and the service refuses new references to deleted rows. Fourth, the table grows forever, so I plan archiving or purging after a retention period.
And finally, soft delete is not erasure: for personal data under laws like GDPR it must be followed by a hard delete or anonymisation.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Hibernate 6, MySQL 8.0
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A table uses a deleted_at column for soft deletes. What problems does this cause for...": https://scrivio-interview-library.netlify.app/databases/data-modeling/419022306f/