SOFT DELETESRestore window, then purgeA customer deletes the wrong project and asks support to bring it back. The usual answer is a soft delete: mark the row deleted, hide it and allow a restore. Done carelessly, it creates the opposite problem: deleted records nobody ever removes. A good design gives users an undo window and the data an end date.
When should a delete be soft, and when hard?
Soft-delete records people build up and might remove by mistake; hard-delete the rest:
- Soft: projects, documents, contacts and other records a customer would miss.
- Hard: sessions, one-time tokens, expired invites and temporary uploads that nobody restores.
- Hard, straight away: anything a person asks you to erase, and anything they remove with "delete forever".
Keep archived separate: customers expect archived records to stay, while deleted ones are on their way out. Sharing one column makes the purge dangerous.
Why a timestamp rather than a deleted flag?
Because the purge needs to know when, and the trash needs to show who. An is_deleted boolean answers neither. A nullable deleted_at stays null for live rows and holds the moment of deletion otherwise; deleted_by records the person. The trash can then say "deleted by Sam three days ago", and a scheduled job can find rows past the window.
How do you keep deleted rows out of everything?
Put the filter in one place every read passes through. A missed filter throws no error; it quietly shows a deleted contact in a dropdown or an export. The rule has to reach:
- List screens, counts and dashboard totals.
- API endpoints, which should return 404 for a deleted record.
- The search index: remove the entry on delete and add it back on restore.
- CSV exports, reports and anything synced to another tool.
- Scheduled jobs, so a deleted project stops sending reminders.
An ORM scope or one data-access layer covers application code; a view of live rows suits reporting tools. With row-level security, create that view with security_invoker = true, or PostgreSQL applies the view owner's permissions and policies instead of the user's.
Why do unique constraints break, and how do you fix them?
A deleted row still holds its value. Delete the project with the slug acme, create another acme, and the unique constraint rejects it. Adding deleted_at to the constraint doesn't help by itself: PostgreSQL treats two nulls as not equal there by default, so two live rows with the same slug would both pass.
The fix is a partial unique index, which PostgreSQL documents: it enforces uniqueness among rows matching its condition and ignores the rest.
ALTER TABLE projects
ADD COLUMN deleted_at timestamptz,
ADD COLUMN deleted_by uuid REFERENCES users (id) ON DELETE SET NULL;
-- replaces the old UNIQUE (workspace_id, slug)
CREATE UNIQUE INDEX projects_live_slug
ON projects (workspace_id, slug)
WHERE deleted_at IS NULL;With ON DELETE SET NULL, erasing the deleter's account later clears deleted_by instead of failing.
How do child records and restores work?
Your code decides both, because a soft delete is an UPDATE, and ON DELETE CASCADE only fires when a row is actually deleted. Choose per relationship: cascade the soft delete, block it while children exist, or hide children through their parent. We usually cascade in one transaction with the parent's exact deleted_at, so a restore brings back that batch, not tasks deleted separately last week. At purge time, hard-delete the parent and an ON DELETE CASCADE foreign key removes the children.
Treat a restore as a write with its own checks:
- Give restore and "delete forever" their own permissions.
- Check for clashes: if a live record now uses the same slug, the index rejects the restore, so offer a rename.
- Restore the parent before its children, or ask where the item should go.
- Add it back to the search index and log who restored it.
How long should deleted data stay, and what about erasure?
Only for a window you state, and an erasure request skips it. A scheduled job hard-deletes rows whose deleted_at is older than the window, in batches, with their files and search entries. In Laravel, the SoftDeletes trait sets deleted_at and hides those rows from queries, and the Prunable trait with a scheduled model:prune command permanently deletes soft-deleted models matching the prune query.
There is a privacy reason too. GDPR's storage limitation principle, Article 5(1)(e), says personal data must not be kept in identifiable form longer than its purpose needs. A trash that never empties is hard to square with that.
And a soft delete is not erasure. Under Article 17, people can have their personal data erased without undue delay when one of its grounds applies, and the UK ICO expects a response within one month. A row marked deleted is still data you hold, so erasure should hard-delete, subject to Article 17(3) exceptions such as data you must keep by law. For backups, the ICO's key issue is putting the data "beyond use" until overwritten on schedule, and telling the person what happens to it. Check the details with your legal adviser.
Test deletion like a feature: delete a record, search for it everywhere it could appear, then restore it after creating a duplicate.
Where to start
List every table users can delete from, mark each soft or hard with a window, then find every place that reads those tables. Our web app and SaaS development service designs deletion alongside the data model, or get in touch for a second look at your app.
FAQ
Frequently asked questions
Can we add soft deletes to an existing app?
Yes, in stages. The column is the easy part; the work is finding every read. Replace unique constraints with partial indexes, built with CREATE UNIQUE INDEX CONCURRENTLY on busy tables so writes keep flowing. Route queries through one scope, then search the codebase for raw SQL, reports and jobs that bypass it. Switch the delete buttons over last.
How many days should items stay in the trash?
Enough for someone to notice a mistake, and no more than you can justify. Pick a fixed number of days per record type, state it in your terms and show it in the trash itself, so nobody is surprised. Records the law requires you to keep follow that requirement instead of the trash window.
Should deleting a whole workspace be a soft delete too?
Usually yes, with a short grace period, because a deleted workspace is the hardest mistake for a customer to undo alone. Lock it at once: block sign-ins, API keys and scheduled jobs, and stop its emails. When the grace period ends, purge everything it owns, including files and search entries, and log that the purge ran.
Does a deleted_at column slow queries down?
Not if your indexes match how you query. PostgreSQL only uses a partial index when the query's WHERE condition implies the index condition, so write deleted_at IS NULL the same way everywhere. Matching happens when the query is planned, so a parameter in place of that condition will not match. A table that is mostly deleted rows usually means the purge is not running.
Can customers empty the trash themselves?
Yes, and it is worth offering. Delete forever should hard-delete straight away, ask for confirmation, sit behind its own permission and appear in your audit log. It also gives a customer a self-service way to remove something sensitive before the window ends, which saves a support ticket and a manual database fix.
