Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to implement soft deletion?

Working on a project at the moment and we have to implement soft deletion for the majority of users (user roles). We decided to add an is_deleted='0' field on each table in the database and set it to '1' if particular user roles hit a delete button on a specific record.

For future maintenance now, each SELECT query will need to ensure they do not include records where is_deleted='1'.

Is there a better solution for implementing soft deletion?

Update: I should also note that we have an Audit database that tracks changes (field, old value, new value, time, user, ip) to all tables/fields within the Application database.

like image 907
Josh Smeaton Avatar asked Sep 16 '08 00:09

Josh Smeaton


People also ask

How do you handle soft delete in SQL?

To restore the soft-deleted DB, it must first be undeleted. To undelete, choose the soft-deleted DB, and then select the option Undelete. A window will appear warning that if undelete is chosen, all restore points for the database will be undeleted and available for performing a restore operation.

How soft delete is implemented in Entity Framework?

The Soft Delete feature allows you to flag entities as deleted (Soft Delete) instead of deleting them physically (Hard Delete). The soft delete feature can be achieved by using the 'IEFSoftDelete' interface. By default, this interface is always added to the manager.

How do you implement soft delete in Rails?

Now, we can soft delete a comment by calling #destroy . In the next step we add the default scope which will exclude all deleted comments. And create only_deleted and with_deleted scopes. Now, we can soft delete comments and query non-deleted and deleted comments.


2 Answers

I would lean towards a deleted_at column that contains the datetime of when the deletion took place. Then you get a little bit of free metadata about the deletion. For your SELECT just get rows WHERE deleted_at IS NULL

like image 124
ctcherry Avatar answered Sep 22 '22 09:09

ctcherry


You could perform all of your queries against a view that contains the WHERE IS_DELETED='0' clause.

like image 23
David J. Sokol Avatar answered Sep 25 '22 09:09

David J. Sokol