Skip to content

Soft Delete

deleted_at, partial indexes, and a live-rows view for safe record deletion.

Soft deletion marks a row as deleted without removing it from the database. The row stays queryable for audits, can be restored without backups, and foreign keys remain intact.

Schema

CREATE TABLE users (
  id         BIGINT      GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  email      TEXT        NOT NULL,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  deleted_at TIMESTAMPTZ
);

-- Uniqueness applies to live rows only; see Trade-offs below.
CREATE UNIQUE INDEX uq_users_email_live ON users (email) WHERE deleted_at IS NULL;

CREATE TRIGGER users_set_updated_at
  BEFORE UPDATE ON users
  FOR EACH ROW
  EXECUTE FUNCTION set_updated_at();

-- Only index live rows; keeps the index small regardless of delete volume.
CREATE INDEX idx_users_live ON users (id) WHERE deleted_at IS NULL;
CREATE TABLE users (
  id         INTEGER NOT NULL PRIMARY KEY,
  email      TEXT    NOT NULL,
  created_at TEXT    NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')),
  updated_at TEXT    NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')),
  deleted_at TEXT
) STRICT;

-- Uniqueness applies to live rows only; see Trade-offs below.
CREATE UNIQUE INDEX uq_users_email_live ON users (email) WHERE deleted_at IS NULL;

CREATE TRIGGER users_set_updated_at
  AFTER UPDATE ON users
  BEGIN
    UPDATE users SET updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
    WHERE id = NEW.id;
  END;

-- Partial index syntax is identical to Postgres (SQLite 3.8.9+).
CREATE INDEX idx_users_live ON users (id) WHERE deleted_at IS NULL;

deleted_at

NULL means live; a timestamp means deleted. The timestamp records when the deletion happened, which is useful for audits and retention policies.

Partial index

A standard index on a soft-delete table indexes every row, including deleted ones that most queries will never touch. A partial index only covers rows where deleted_at IS NULL, so it stays small and fast regardless of delete volume.

The WHERE deleted_at IS NULL syntax is identical in Postgres and SQLite (3.8.9+).

Live-rows view

CREATE VIEW live_users AS
  SELECT * FROM users WHERE deleted_at IS NULL;

Application code queries live_users instead of users directly. The view acts as a guardrail: it’s harder to accidentally return deleted rows than it is to forget a WHERE deleted_at IS NULL clause in every query.

Common queries

-- soft delete
UPDATE users SET deleted_at = now() WHERE id = $1;

-- restore
UPDATE users SET deleted_at = NULL WHERE id = $1;

-- all deleted rows
SELECT * FROM users WHERE deleted_at IS NOT NULL;

-- deleted in the last 30 days
SELECT * FROM users WHERE deleted_at > now() - INTERVAL '30 days';
-- soft delete
UPDATE users SET deleted_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now') WHERE id = ?;

-- restore
UPDATE users SET deleted_at = NULL WHERE id = ?;

-- all deleted rows
SELECT * FROM users WHERE deleted_at IS NOT NULL;

-- deleted in the last 30 days
SELECT * FROM users WHERE deleted_at > datetime('now', '-30 days');

Trade-offs

Foreign keys still hold. A soft-deleted row can still be referenced by other tables. Whether that’s correct depends on your domain: a deleted user still being the author of their blog_posts is usually fine; a deleted product still appearing in new order_items is probably not.

Unique constraints include deleted rows. A plain UNIQUE (email) constraint covers deleted rows too: soft-delete a user and no new user can ever reuse that email. This is why the schema above declares uniqueness as a partial unique index (WHERE deleted_at IS NULL) instead of a column constraint; the syntax is identical in Postgres and SQLite.

Storage grows indefinitely. Soft-deleted rows are never removed unless you explicitly purge them. For high-write tables, add a periodic job to hard-delete rows older than your retention window.

Type to search…

↑↓ navigate↵ selectEsc close