Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära 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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 6

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you explain the pros and cons of using soft deletes versus hard deletes?

How do I restore a soft-deleted record back to active status?

What are some best practices for archiving and purging soft-deleted records?

bookSoft Deletes and Data Archiving

Svep för att visa menyn

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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 6
some-alt