Soft delete? Why?
Oops !! Accidentally deleted something which you are not supposed to? 😬
Soft delete is a concept used in software development to handle the deletion of records or data in a way that allows for potential recovery of the deleted items. Unlike a traditional (hard) delete, where data is permanently removed from the database, soft delete involves marking a record as deleted without actually removing it. This can be useful in various scenarios, such as:
- Accidental Deletion Prevention: Soft delete helps prevent accidental data loss. If a user or a system administrator mistakenly deletes important data, it can be restored easily since the data is not physically removed from the database.
- Auditing and Compliance: In applications where data auditing and compliance are important, soft delete enables you to maintain a record of when and why a particular record was deleted. This can be valuable for tracking changes and meeting regulatory requirements.
- Recovery and Undo: Soft deleted data can be recovered if needed. This can be especially useful when a user realizes they deleted something they shouldn’t have or if there’s a change in business requirements that requires restoring deleted records.
- User Experience: Soft delete allows you to give users the perception of data permanence while still providing the ability to recover deleted items. This can improve the user experience by reducing anxiety about accidentally deleting data.
How to implement soft delete?
You can implement soft delete by adding an additional column to your database table, often named something like deleted_at
. When a record is deleted, instead of removing it from the database, you set the deleted_at
column to the current timestamp. When querying the database, you can then exclude records with a non-null deleted_at
value to effectively hide the soft-deleted items from most queries.
Here’s a simplified example of how you might implement soft delete in a PHP application using a MySQL database:
// Soft delete a record
$idToDelete = 5;
$query = "UPDATE your_table SET deleted_at = NOW() WHERE id = :id";
// Execute the query and bind the parameter
// Retrieve records excluding soft deleted items
$query = "SELECT * FROM your_table WHERE deleted_at IS NULL";
// Execute the query
// Restore a soft deleted record
$idToRestore = 5;
$query = "UPDATE your_table SET deleted_at = NULL WHERE id = :id";
// Execute the query and bind the parameter
Remember that implementing soft delete requires careful consideration and planning, especially in terms of data integrity, query logic, and how you handle related records. It’s also important to communicate to your application’s users how soft delete works and how they can recover deleted data if needed.
Some might find using extra column in the main table for soft delete controversial, So what’s the alternative way?
Here are some ways:
- Backing up data continuously, use backtrack functionality if database supports.
- Implementing soft delete using archive tables involves creating a separate table to hold the soft-deleted records while keeping the main table clean and free of deleted data.
Enjoyed reading this article? Give it a round of applause by clapping 👏 and share your thoughts in the comments section below. Your feedback and engagement are what keep writers inspired and motivated to create more content you love!