Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Soft Deletes and Data Archiving | Advanced Database Design Patterns
Database Design Patterns

bookSoft Deletes and Data Archiving

Soft deletes are a strategy in database design where records are not physically removed from a table when deleted. Instead, a special column—often a timestamp or boolean flag—is used to mark records as deleted. This approach is favored over hard deletes, which permanently remove data, because it allows you to retain historical information, enable data recovery, and maintain referential integrity for related records. For instance, if you delete a project but want to keep its associated tasks and audit trails, soft deletes make this possible without losing valuable context.

-- Add a 'deleted_at' column to the 'projects' table for soft deletes
ALTER TABLE projects
ADD COLUMN deleted_at TIMESTAMP NULL;

Once a deleted_at column is present, you can distinguish between active and soft-deleted records by checking whether this column is NULL or contains a timestamp. Active records have deleted_at set to NULL, while deleted records have a timestamp indicating when they were marked as deleted. This pattern also enables archiving: you can periodically move old or soft-deleted records to a separate archive table, reducing clutter in your main tables and improving query performance. Archiving is especially useful for managing data lifecycle and compliance requirements.

-- Query only active (not soft-deleted) projects
SELECT *
FROM projects
WHERE deleted_at IS NULL;

-- Query only soft-deleted projects
SELECT *
FROM projects
WHERE deleted_at IS NOT NULL;

-- Archive projects soft-deleted more than 1 year ago
INSERT INTO projects_archive
SELECT *
FROM projects
WHERE deleted_at IS NOT NULL
  AND deleted_at < NOW() - INTERVAL '1 year';

-- Remove archived projects from main table
DELETE FROM projects
WHERE deleted_at IS NOT NULL
  AND deleted_at < NOW() - INTERVAL '1 year';

1. What is a soft delete?

2. How do you implement a soft delete in a table?

3. What is a benefit of using soft deletes over hard deletes?

question mark

What is a soft delete?

Select the correct answer

question mark

How do you implement a soft delete in a table?

Select the correct answer

question mark

What is a benefit of using soft deletes over hard deletes?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 6

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

bookSoft Deletes and Data Archiving

Sveip for å vise menyen

Soft deletes are a strategy in database design where records are not physically removed from a table when deleted. Instead, a special column—often a timestamp or boolean flag—is used to mark records as deleted. This approach is favored over hard deletes, which permanently remove data, because it allows you to retain historical information, enable data recovery, and maintain referential integrity for related records. For instance, if you delete a project but want to keep its associated tasks and audit trails, soft deletes make this possible without losing valuable context.

-- Add a 'deleted_at' column to the 'projects' table for soft deletes
ALTER TABLE projects
ADD COLUMN deleted_at TIMESTAMP NULL;

Once a deleted_at column is present, you can distinguish between active and soft-deleted records by checking whether this column is NULL or contains a timestamp. Active records have deleted_at set to NULL, while deleted records have a timestamp indicating when they were marked as deleted. This pattern also enables archiving: you can periodically move old or soft-deleted records to a separate archive table, reducing clutter in your main tables and improving query performance. Archiving is especially useful for managing data lifecycle and compliance requirements.

-- Query only active (not soft-deleted) projects
SELECT *
FROM projects
WHERE deleted_at IS NULL;

-- Query only soft-deleted projects
SELECT *
FROM projects
WHERE deleted_at IS NOT NULL;

-- Archive projects soft-deleted more than 1 year ago
INSERT INTO projects_archive
SELECT *
FROM projects
WHERE deleted_at IS NOT NULL
  AND deleted_at < NOW() - INTERVAL '1 year';

-- Remove archived projects from main table
DELETE FROM projects
WHERE deleted_at IS NOT NULL
  AND deleted_at < NOW() - INTERVAL '1 year';

1. What is a soft delete?

2. How do you implement a soft delete in a table?

3. What is a benefit of using soft deletes over hard deletes?

question mark

What is a soft delete?

Select the correct answer

question mark

How do you implement a soft delete in a table?

Select the correct answer

question mark

What is a benefit of using soft deletes over hard deletes?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 6
some-alt