Soft Deletes and Their Costs in Relational Databases
What I ran into with a deleted_at column on every table: broken unique constraints, easy-to-forget filters, bigger indexes, and an archive-table alternative.
· 3 min read · Erik Kubica
Why I'm Careful With Soft Deletes
In many web frameworks (Laravel, Ruby on Rails, Django, TypeORM) there is a convenient trait or mixin called SoftDeletes.
You add a nullable deleted_at timestamp column to your table, include the trait on your model, and you're done. When someone calls $user->delete(), the framework executes UPDATE users SET deleted_at = NOW() instead of a real SQL DELETE.
The appeal is obvious: you won't lose customer data by accident, and deletes can be undone.
I've been maintaining production databases for 13 years, and soft deletes have caused me more trouble than I expected. I now treat them as something to opt into for specific tables, not a default for all of them.
I find it easier to reason about a table when it holds only the current, active records, and history lives somewhere else.
Problem 1: Unique Constraints
As soon as you add deleted_at, standard database constraints stop doing what you expect.
Suppose you have a users table where email must be unique. A customer deletes their account. Two months later, they try to register again with the same email address.
Your database throws a unique constraint violation error:
ERROR: duplicate key value violates unique constraint "users_email_key"
DETAIL: Key (email)=(erik@example.com) already exists.
The old row is still there with deleted_at = '2026-03-12'.
The usual fix is to replace the plain constraint with a partial index:
-- Forcing partial indexes because of soft deletes
DROP INDEX users_email_key;
CREATE UNIQUE INDEX idx_users_email_active
ON users (email)
WHERE deleted_at IS NULL;
That works, but you then have to repeat it for every unique key and composite constraint, across every table that has soft deletes.
Problem 2: Every Query Needs a Filter
Every query in your codebase now needs an explicit condition:
SELECT * FROM orders WHERE user_id = 42 AND deleted_at IS NULL;
If you miss that AND deleted_at IS NULL once in a complex subquery, an inner join, or a reporting aggregation, deleted records can quietly show up in things like accounting exports. Framework scopes help, but raw SQL and reports tend to bypass them.
Indexes also grow. If 40% of your rows are "deleted", your B-tree indexes are larger than they need to be, which can push active pages out of PostgreSQL's shared_buffers and cause more disk I/O. How much this matters depends on your data size and how many rows end up deleted.
An Alternative: An Archive Table
If you need audit history or data retention for compliance, one option is to keep it out of your main transactional tables and use an archive table.
When a record is deleted, it is really deleted from the active table, and a simple database trigger copies the payload into the archive:
-- Step 1: Create a dedicated archive table
CREATE TABLE users_archive (
id bigint NOT NULL,
email text NOT NULL,
profile_data jsonb,
deleted_at timestamptz DEFAULT now(),
deleted_by text
);
-- Step 2: Automatically move deleted records on hard DELETE
CREATE OR REPLACE FUNCTION archive_deleted_user()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO users_archive (id, email, profile_data, deleted_at)
VALUES (OLD.id, OLD.email, OLD.profile_data, now());
RETURN OLD;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER trg_archive_user
BEFORE DELETE ON users
FOR EACH ROW EXECUTE FUNCTION archive_deleted_user();
What I like about this approach:
- Clean queries: The application writes plain SQL:
SELECT * FROM users WHERE id = 42;. Nodeleted_at IS NULLanywhere. - Normal constraints: A regular
UNIQUE(email)works out of the box, and foreign keys cascade as usual. - Smaller indexes: Indexes on the active table only cover active rows.
- Audit trail: Compliance and customer service teams can query
users_archivewhen they need history.
Soft deletes can still be the right call, for example when users need an "undo" or restore button, or when related records must keep pointing at the deleted row. In that case, it helps to be deliberate about it and use partial indexes and default scopes from the start. Otherwise, the archive table has kept things simpler for me.