The two key concepts Soft-delete
and Hard-delete
plays a crucial role in Database designing. Soft-delete marks the record as inactive/deleted whereas hard-delete completely removes the record from the database. Understanding these techniques helps in managing data effectively. Let's explore more of it.
#. Soft-delete vs. Hard-delete
Soft-delete is a technique where instead of permanently removing a record from a database, we mark it as deleted
by setting a flag or a status. Which means, data remains in the database, but considered inactive/deleted. It is often reversible, making it easier to recover data if needed.
Hard-delete on the other hand is the traditional way of permanently removing a record from a database. Once a record is hard-deleted, it cant be recovered unless there are backups.
#. Implementation
In soft-delete implementation, a common approach is to set a deleted
flag to the database table. So whenever a record gets deleted, set this flag to true. Then the application filters out the records with deleted
flag true to ensure deleted records aren't included in the operations.
This flag can be a boolean or date-time field. In case of date-time field, we can also get deleted timestamp of that record.
#. Advantages & Disadvantages
Soft-delete | Hard-delete |
---|---|
Deleted record can be easily recovered if needed. | Cannot recover, once deleted it's gone forever. |
Protects against accidental data loss due to unintended actions. | Removes data once deleted, saving storage space. |
Can lead to large data database size, since data remains in database. | Removes unnecessary records, saving storage space. |
Might requires more complex queries to filter out deleted records. | Nothing complex, just remove. |
From these points, storage space requires more attention. That means, not everything requires soft-delete and only use it when you need to keep a record of deleted records and easily recover them later. Otherwise go for hard-delete when it's no longer needed.
Also note: soft-delete can slow down queries in large dataset, because database needs to filter out delete records.