Database Design Patterns · Lesson 73 of 112

Database Soft Delete and Archiving

What you will learn

  • What soft delete is and why people use it
  • How to hide deleted rows with a view
  • The unique key trap and how a partial index solves it
  • When archiving is better

A normal DELETE removes a row for good (unless you restore a backup, lesson 8.3). Sometimes you want a safer way. The examples run on the sample database you chose in lesson 4.1 (use the Example data picker at the top), build on each other from top to bottom, and create tables of their own in your browser's copy; Reset data removes them.

1. Soft delete

Soft delete means marking a row as deleted instead of removing it. The usual way is a nullable timestamp column, deleted_at: NULL means alive; a time means deleted at that moment.

Soft delete the second row, then read with and without the filter. (The table is new and holds the first two rows of the sample data, so it is safe to play with.)

CREATE TABLE members (id INTEGER PRIMARY KEY, email TEXT NOT NULL, name TEXT, deleted_at TEXT);
INSERT INTO members (email, name) SELECT email, first_name FROM customers ORDER BY customer_id LIMIT 2;
UPDATE members SET deleted_at = '2026-03-02 10:00:00' WHERE id = 2;
SELECT id, name, deleted_at FROM members ORDER BY id;
SELECT id, name FROM members WHERE deleted_at IS NULL ORDER BY id;
id  name   deleted_at
--  -----  -------------------
 1  Vik    NULL
 2  Wanda  2026-03-02 10:00:00
(2 rows)

id  name
--  ----
 1  Vik
(1 row)
CREATE TABLE subscribers (id INTEGER PRIMARY KEY, email TEXT NOT NULL, name TEXT, deleted_at TEXT);
INSERT INTO subscribers (email, name) SELECT lower(replace(full_name, ' ', '.')) || '@example.com', full_name FROM customers ORDER BY customer_id LIMIT 2;
UPDATE subscribers SET deleted_at = '2026-03-02 10:00:00' WHERE id = 2;
SELECT id, name, deleted_at FROM subscribers ORDER BY id;
SELECT id, name FROM subscribers WHERE deleted_at IS NULL ORDER BY id;
id  name        deleted_at
--  ----------  -------------------
 1  Chen Park   NULL
 2  Rafi Uddin  2026-03-02 10:00:00
(2 rows)

id  name
--  ---------
 1  Chen Park
(1 row)
CREATE TABLE club_members (id INTEGER PRIMARY KEY, email TEXT NOT NULL, name TEXT, deleted_at TEXT);
INSERT INTO club_members (email, name) SELECT email, first_name FROM students ORDER BY student_id LIMIT 2;
UPDATE club_members SET deleted_at = '2026-03-02 10:00:00' WHERE id = 2;
SELECT id, name, deleted_at FROM club_members ORDER BY id;
SELECT id, name FROM club_members WHERE deleted_at IS NULL ORDER BY id;
id  name   deleted_at
--  -----  -------------------
 1  Vera   NULL
 2  Elena  2026-03-02 10:00:00
(2 rows)

id  name
--  ----
 1  Vera
(1 row)

Reasons to do this: the user can undo a mistake, old orders can still point to the customer, and you keep a trace. The cost: every query must remember WHERE deleted_at IS NULL, and one forgotten filter shows deleted data. A view removes the risk for everyday reading (lesson 4.7). The view below is built on the table you just made:

CREATE VIEW active_members AS SELECT id, email, name FROM members WHERE deleted_at IS NULL;
SELECT id, name FROM active_members ORDER BY id;
id  name
--  ----
 1  Vik
(1 row)
CREATE VIEW active_subscribers AS SELECT id, email, name FROM subscribers WHERE deleted_at IS NULL;
SELECT id, name FROM active_subscribers ORDER BY id;
id  name
--  ---------
 1  Chen Park
(1 row)
CREATE VIEW active_club_members AS SELECT id, email, name FROM club_members WHERE deleted_at IS NULL;
SELECT id, name FROM active_club_members ORDER BY id;
id  name
--  ----
 1  Vera
(1 row)

Let the application read the view, and use the base table only for restoring and reports.

Quick check: What does soft delete do to a row, compared with a real `DELETE`?

It keeps the row and marks it as deleted (for example with a deleted_at value), so it can be restored, but queries must remember to hide it.

2. The unique key trap

Suppose emails must be unique (lesson 2.6). The second row was soft-deleted. Now the same person signs up again with the same email:

The table copies the first two customers of the sample data (their emails are already unique there). Row 2 is the soft-deleted one. Now create the plain unique index, and let the same email sign up again:

CREATE UNIQUE INDEX uq_email_all ON members(email);
(done)
INSERT INTO members (email, name) SELECT email, 'Signed up again' FROM members WHERE id = 2;
IntegrityError: UNIQUE constraint failed: members.email

The table copies the first two customers of the sample data (their emails are already unique there). Row 2 is the soft-deleted one. Now create the plain unique index, and let the same email sign up again:

CREATE UNIQUE INDEX uq_email_all ON subscribers(email);
(done)
INSERT INTO subscribers (email, name) SELECT email, 'Signed up again' FROM subscribers WHERE id = 2;
IntegrityError: UNIQUE constraint failed: subscribers.email

The table copies the first two students of the sample data (their emails are already unique there). Row 2 is the soft-deleted one. Now create the plain unique index, and let the same email sign up again:

CREATE UNIQUE INDEX uq_email_all ON club_members(email);
(done)
INSERT INTO club_members (email, name) SELECT email, 'Signed up again' FROM club_members WHERE id = 2;
IntegrityError: UNIQUE constraint failed: club_members.email

The deleted row still holds the email, so the "unique" rule blocks a legitimate signup. The fix is a partial unique index: uniqueness only among rows where deleted_at IS NULL.

DROP INDEX uq_email_all;
CREATE UNIQUE INDEX uq_email_active ON members(email) WHERE deleted_at IS NULL;
INSERT INTO members (email, name) SELECT email, 'Signed up again' FROM members WHERE id = 2;
SELECT id, name, deleted_at FROM members WHERE id IN (2, 3) ORDER BY id;
id  name             deleted_at
--  ---------------  -------------------
 2  Wanda            2026-03-02 10:00:00
 3  Signed up again  NULL
(2 rows)

A third signup with that email, while row 3 is alive, must still fail:

INSERT INTO members (email, name) SELECT email, 'Third try' FROM members WHERE id = 3;
IntegrityError: UNIQUE constraint failed: members.email
DROP INDEX uq_email_all;
CREATE UNIQUE INDEX uq_email_active ON subscribers(email) WHERE deleted_at IS NULL;
INSERT INTO subscribers (email, name) SELECT email, 'Signed up again' FROM subscribers WHERE id = 2;
SELECT id, name, deleted_at FROM subscribers WHERE id IN (2, 3) ORDER BY id;
id  name             deleted_at
--  ---------------  -------------------
 2  Rafi Uddin       2026-03-02 10:00:00
 3  Signed up again  NULL
(2 rows)

A third signup with that email, while row 3 is alive, must still fail:

INSERT INTO subscribers (email, name) SELECT email, 'Third try' FROM subscribers WHERE id = 3;
IntegrityError: UNIQUE constraint failed: subscribers.email
DROP INDEX uq_email_all;
CREATE UNIQUE INDEX uq_email_active ON club_members(email) WHERE deleted_at IS NULL;
INSERT INTO club_members (email, name) SELECT email, 'Signed up again' FROM club_members WHERE id = 2;
SELECT id, name, deleted_at FROM club_members WHERE id IN (2, 3) ORDER BY id;
id  name             deleted_at
--  ---------------  -------------------
 2  Elena            2026-03-02 10:00:00
 3  Signed up again  NULL
(2 rows)

A third signup with that email, while row 3 is alive, must still fail:

INSERT INTO club_members (email, name) SELECT email, 'Third try' FROM club_members WHERE id = 3;
IntegrityError: UNIQUE constraint failed: club_members.email

Now one deleted and one live row share the email, but two live rows cannot. Partial indexes exist in SQLite, PostgreSQL and some other systems; others need a different trick, so check your own system.

3. Other soft delete traps

  • Foreign keys still point at the soft-deleted row. That is often the point, but then "show this customer's orders" must decide what to show.
  • Privacy. Soft delete keeps personal data. If a person has a legal right to have their data erased (lesson 8.6), hiding is not enough; you must really remove or anonymise it.
  • Tables grow forever. Plan a cleanup for rows deleted long ago.

4. Archiving: move, do not hide

Instead of marking old rows, move them to another table (or database) so the main table stays small and fast.

CREATE TABLE members_archive (id INTEGER, email TEXT, name TEXT, archived_at TEXT);
BEGIN;
INSERT INTO members_archive SELECT id, email, name, '2026-03-05' FROM members WHERE id = 1;
DELETE FROM members WHERE id = 1;
COMMIT;
SELECT id, name FROM members ORDER BY id;
SELECT id, name, archived_at FROM members_archive;
id  name
--  ---------------
 2  Wanda
 3  Signed up again
(2 rows)

id  name  archived_at
--  ----  -----------
 1  Vik   2026-03-05
(1 row)
CREATE TABLE subscribers_archive (id INTEGER, email TEXT, name TEXT, archived_at TEXT);
BEGIN;
INSERT INTO subscribers_archive SELECT id, email, name, '2026-03-05' FROM subscribers WHERE id = 1;
DELETE FROM subscribers WHERE id = 1;
COMMIT;
SELECT id, name FROM subscribers ORDER BY id;
SELECT id, name, archived_at FROM subscribers_archive;
id  name
--  ---------------
 2  Rafi Uddin
 3  Signed up again
(2 rows)

id  name       archived_at
--  ---------  -----------
 1  Chen Park  2026-03-05
(1 row)
CREATE TABLE club_members_archive (id INTEGER, email TEXT, name TEXT, archived_at TEXT);
BEGIN;
INSERT INTO club_members_archive SELECT id, email, name, '2026-03-05' FROM club_members WHERE id = 1;
DELETE FROM club_members WHERE id = 1;
COMMIT;
SELECT id, name FROM club_members ORDER BY id;
SELECT id, name, archived_at FROM club_members_archive;
id  name
--  ---------------
 2  Elena
 3  Signed up again
(2 rows)

id  name  archived_at
--  ----  -----------
 1  Vera  2026-03-05
(1 row)

The copy and the delete sit between BEGIN and COMMIT, one transaction (lesson 6.2), so a crash cannot lose the row or keep it twice.

Soft delete Archive
Undo one UPDATE copy back
Main table size keeps growing stays small
Every query must filter yes (or use a view) no
Good for "oops" recovery, keeping links old, rarely used history

Worked example: a small shop's customers

Customers sometimes ask the shop to close their account, and may come back later. The shop uses soft delete with a view for the website, plus a partial unique index on email. Orders older than five years are moved to orders_archive once a year, in a transaction. If a customer demands erasure under a privacy law, the shop anonymises the row (name and email replaced) rather than only hiding it. The shop is made up.

Common mistakes

  • Forgetting the filter in one query. Use a view.
  • A plain UNIQUE constraint that blocks reuse after a soft delete.
  • Treating soft delete as erasure of personal data.
  • Soft-deleting everything by habit, including tables where a real delete is fine.

Key takeaways

  • Soft delete marks rows (deleted_at) instead of removing them, allowing undo.
  • Read through a view so deleted rows stay hidden.
  • Use a partial unique index so deleted rows do not block new ones.
  • Archiving moves old rows out of the main table; do the move in one transaction.
  • Hiding is not the same as erasing personal data.

Practice

1. What does deleted_at IS NULL mean?

Show answer

The row has not been deleted.

2. Why use a view for soft-deleted tables?

Show answer

Queries cannot forget the filter, because the view always hides deleted rows.

3. A plain unique index on email stops a returning user from signing up again after soft delete. Why, and what fixes it?

Show answer

The deleted row still holds the email. A partial unique index on rows where deleted_at IS NULL fixes it.

4. Why is moving rows to an archive wrapped in a transaction?

Show answer

So the copy and delete succeed or fail together; otherwise a failure could lose the row or leave it in both tables.

5. When is archiving better than soft delete?

Show answer

For old rows that are rarely needed, because the main table stays small and queries need no extra filter.

6. A user asks you to erase their data. Is soft delete enough?

Show answer

No. The personal data is still stored. Remove or anonymise it for real (lesson 8.6).