Quick revision
The short answer
Each symptom has its own fix. Re-registration fails because the unique index still covers deleted rows, so we make it a partial index on live rows (or use a key that deleted rows cannot collide on). Reports are wrong because native SQL ignores the ORM filter, so we give reporting a view of active rows and document the rule. The table grows because nothing ever leaves, so we purge or archive soft-deleted rows after a retention period, which GDPR requires for personal data anyway, and we ask whether soft delete was the right tool at all.
What to remember
In this answer, we traced all three symptoms to one fact: the soft-delete filter lives in Hibernate's SQL generation, not in the database. We fixed re-registration with a partial unique index on live rows, or NULLS NOT DISTINCT or a generated column where needed; fixed reports with a view of active rows and a documented rule for native queries; stopped the growth with a batched purge and archive job driven by a retention period and GDPR; aligned indexes with the live-row filter; and asked whether audit tables or Envers would serve the original purpose better.
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.
Each symptom has its own fix. Re-registration fails because the unique index still covers deleted rows, so we make it a partial index on live rows (or use a key that deleted rows cannot collide on). Reports are wrong because native SQL ignores the ORM filter, so we give reporting a view of active rows and document the rule. The table grows because nothing ever leaves, so we purge or archive soft-deleted rows after a retention period, which GDPR requires for personal data anyway, and we ask whether soft delete was the right tool at all.
Why this question is asked
Soft delete is often added in an afternoon: one annotation, and deleted customers disappear from the application. The problems appear weeks later, in places that the annotation does not reach. This scenario is popular in interviews because it tests whether we understand where the soft-delete filter actually lives and what it leaves out. In this answer, we will recap how soft delete works, then fix the three symptoms one at a time (the blocked email, the wrong reports and the growing table), look at indexing for a table full of deleted rows, and finally question whether soft delete should stay.
1A Quick Recap: Where the Filter Lives
With Hibernate's @SoftDelete, or the older @SQLDelete plus @SQLRestriction pair, a delete becomes UPDATE customer SET deleted = true, and Hibernate adds deleted = false to the SQL it generates for the entity. The important word is generates. The filter is part of Hibernate's query building. The database knows nothing about it: to the database, a deleted customer is an ordinary row with a flag set.
So everything the database does on its own, such as enforcing unique constraints, and everything that reaches the database without Hibernate, such as a reporting tool or a native query, still sees deleted rows. All three symptoms in the question come from this one fact.
2Fix 1: Letting a Customer Register Again
The email column has a unique constraint. When a customer deletes their account, the row stays with its email, so a new registration with the same address violates the constraint. We want uniqueness only among live customers.
Notice the alternative carefully, because the common advice of putting deleted_at in the unique key has a trap. Unique indexes normally treat NULLs as distinct from each other, so two live rows with the same email and deleted_at NULL would both be accepted, and we would lose uniqueness for live customers entirely. PostgreSQL 15 and later fix that with NULLS NOT DISTINCT: two live rows collide, while deleted rows have different timestamps and do not. MySQL 8.4 has neither partial indexes nor that option, so there we add a generated column, for example live_email that equals email when deleted_at is null and is null otherwise, and put the unique index on it.
Also check the application side: if registration first calls a repository method like existsByEmail, that query is filtered by Hibernate and agrees with the new index, which is what we want.
3Fix 2: Making Reports Count Only Active Customers
The reports are built with native SQL or a BI tool that reads tables directly, so SELECT count(*) FROM customer includes every deleted customer. We cannot rely on every analyst remembering to add a filter, so we give them something that is correct by default.
Reporting tools are pointed at the view, and the raw table is kept for the few reports that genuinely need deleted rows, such as churn analysis. Inside the application, every native query in a repository must add the condition itself, and we document that rule next to the entity and check it in code review. A test that inserts a soft-deleted customer and asserts that each report query ignores it catches regressions cheaply.
4Fix 3: Purging and Archiving Old Deleted Rows
Soft delete means nothing ever leaves the table, so it only grows. Worse, for personal data it may be illegal to keep it: under GDPR, a customer who asks to be erased expects their data gone, not flagged. So we decide a retention period per purpose, for example 30 days for undo and seven years for financial records that must be kept, and run a purge job.
- Customer deletes accountdeleted_at =
now() - Undo window30 days, row hidden by the ORM filter
- Must some data be kept by law?YesCopy minimal fields to archiveNoSkip archive
- Purge jobhard delete in batches of 5,000
- Row goneemail free, storage reclaimed
5 steps in order. Play them, or step through them one at a time.
Soft delete becomes a waiting room with an exit, not a permanent state.
Where some data must be kept for legal reasons, we first copy it to an archive table or cold storage with personal fields removed or minimised, then purge the original. Child rows with foreign keys to the customer must be handled in the same job, children first, or the delete fails on the constraint.
5Indexing a Table Full of Deleted Rows
Once the filter deleted_at IS NULL appears in nearly every query, indexes should reflect it. If most rows are active, the flag on its own is a poor index because almost every row matches. What helps is putting the flag together with the columns we really filter by, or making the common indexes partial, for example CREATE INDEX ix_customer_country_live ON customer (country) WHERE deleted_at IS NULL. If instead most rows end up deleted before the purge, partial indexes on live rows become small and fast, which is another argument for them.
6Should We Keep Soft Delete at All?
It is worth stepping back and asking what soft delete was for. If the goal was undo, a short window followed by a real delete is enough. If the goal was audit, an audit table or Hibernate Envers records every change, including who deleted what, without leaving dead rows in the live table where every query must avoid them. If the goal was keeping foreign keys valid for old orders, a status such as CLOSED on the customer may describe the business better than deleted. Sometimes the best fix for all three symptoms is replacing soft delete with the tool that matches the purpose.
7Follow-up questions
Why not just use deleted = true and deleted = false in the unique key?
Because then only one deleted row per email can exist: a customer who registers and deletes twice hits the constraint on the second delete. A timestamp or the row id in the key, or a partial index, avoids that.
How do we test that purging is safe?
We run the purge job in an integration test with a customer who has orders, invoices and audit records, and assert that everything that should go is gone, that archived fields are present, and that nothing outside the retention window was touched. We also dry-run it in staging with counts before enabling deletion in production.
Do we need to purge backups too for GDPR?
Backups usually follow their own retention, and regulators generally accept that erased data disappears from backups as they expire, provided restored backups re-apply erasures. That policy should be written down with the data protection officer, so check your legal guidance.
8Conclusion
In this answer, we traced all three symptoms to one fact: the soft-delete filter lives in Hibernate's SQL generation, not in the database. We fixed re-registration with a partial unique index on live rows, or NULLS NOT DISTINCT or a generated column where needed; fixed reports with a view of active rows and a documented rule for native queries; stopped the growth with a batched purge and archive job driven by a retention period and GDPR; aligned indexes with the live-row filter; and asked whether audit tables or Envers would serve the original purpose better.
Open with
All three problems come from the same thing: the soft-delete filter only exists in the SQL Hibernate generates, so the database itself and any native SQL still see deleted rows.
Then, if they want more
For the email, I'd drop the plain unique constraint and create a partial unique index on email where deleted_at is null, on PostgreSQL.
I'd be careful with the idea of just adding deleted_at to the key, because NULLs are distinct in unique indexes, so two live rows would both be allowed; on PostgreSQL 15 NULLS NOT DISTINCT fixes that, and on MySQL I'd use a generated column that's null for deleted rows.
For reports, I'd give reporting tools a view of active customers, document that every native query must filter deleted rows, and add a test with a deleted customer for each report.
For growth, I'd define retention per purpose, say 30 days for undo, archive any fields we're legally required to keep with personal data minimised, and run a batched purge job, which GDPR expects anyway for personal data. I'd also index the columns we filter on with the live condition, using partial indexes when most rows are active.
And finally I'd ask why we have soft delete: if it's for audit, Envers or an audit table may be the better design.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 15, MySQL 8.4
- 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
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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 "After introducing soft delete, a customer cannot register again with the same email...": https://scrivio-interview-library.netlify.app/databases/jpa-fundamentals/5a2277f46b/