DEV Community

Cover image for Hard Delete Vs Soft Delete in DBMS
Rishav Kapil
Rishav Kapil

Posted on

Hard Delete Vs Soft Delete in DBMS

When working with databases, we often need to remove records. There are two common approaches for deletion: hard delete and soft delete. Let’s explore these concepts in simple terms with a real-world example.

Hard Delete

A hard delete means permanently removing a record from the database.
Once deleted, the data is gone forever and cannot be retrieved.

Real-World Example:
Imagine you have an email inbox. If you delete an email and then empty your trash folder, that email is permanently removed. You cannot restore it anymore. This is an example of a hard delete.

How It Works in a Database:

When you perform a hard delete, the database removes the record completely:

DELETE FROM Users WHERE id = 101;
Enter fullscreen mode Exit fullscreen mode

This query deletes the user with ID 101 from the Users table, and the data is no longer available.

Soft Delete

A soft delete means marking a record as deleted without actually removing it from the database. This allows you to "restore" the record later if needed.

Real-World Example:

Think about your recycle bin on a computer. When you delete a file, it goes into the recycle bin instead of being permanently erased. You can still recover the file until you permanently delete it from the recycle bin. This is similar to a soft delete.

How It Works in a Database:

Instead of removing the record, we add a flag (usually a column like is_deleted) to mark it as deleted:

UPDATE Users SET is_deleted = 1 WHERE id = 101;
Enter fullscreen mode Exit fullscreen mode

The record is still in the database, but the is_deleted column indicates it should not be shown in normal operations.

To retrieve only active records, you add a condition in your query:

SELECT * FROM Users WHERE is_deleted = 0;
Enter fullscreen mode Exit fullscreen mode

When to Use Hard Delete

  • When the data is no longer needed.
  • For sensitive data that must be permanently erased (e.g., user passwords).

When to Use Soft Delete

  • When you want to maintain a history of data.

  • For audit logs or when users can undo deletions (e.g., social media posts).

Top comments (1)

Collapse
 
jabkaskin profile image
Jabka Skin

Good post!