Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Recover database records with soft deletion

Wednesday, July 26th, 2017 by Servage

trash-deleteSome websites allow you to delete your content, such as videos you have uploaded or even your whole user account. At least this is what it looks like for the end user. However, in most cases, the content is not actually deleted, which gives administrators an easy way to undo actions of users who make hasty decisions. This can be done using a technique called soft deletion.

How Soft Deletion Works

Soft deletion is incredibly simple to set up. All you need is one additional column in your database. The field is usually called deleted_at or is_deleted, depending on whether you want to know when a specific record was deleted. The deleted_at field stores the exact date and time when a record was deleted while is_deleted only stores a boolean value depending on if the record has been deleted or not.

When a record is deleted, you populate the deleted_at field with the current timestamp or the is_deleted field with 1 if the field is a TINYINT which is suitable for storing boolean values.

The most difficult part is querying only the records that are not deleted from the database. When using soft deletion, you have to alter all of your existing database queries to include WHERE deleted_at = null or WHERE is_deleted = 0. Fortunately, many frameworks can do this for you without having to edit all queries manually. For example, Laravel’s Eloquent supports global scopes, which can be used to filter out soft deleted fields easily.

Is Soft Deletion a Good Idea?

There are arguments for and against soft deletion. The main advantage in soft deletion is the ability to be quickly able to restore data back to an application without having to use backups. The way how soft deletion works can also be customized. You can for instance implement a recycle bin feature where users can mark a record for deletion. The record will be deleted after a certain time period during which the deletion process can be canceled by the user.

The disadvantages of soft deletion include the database becoming larger in size because deleted records take as much space as undeleted records. Since databases don’t have native support for soft deletion, you have to handle all cascading actions manually. For example, if you soft delete a user, you might want to soft delete all of the user’s blog posts too.

Although there are some disadvantages to consider, if it’s possible to delete data in your application, you should consider using soft deletion and evaluate whether the advantages outweigh the disadvantages in your application.

Recover database records with soft deletion, 5.0 out of 5 based on 2 ratings
Categories: Guides & Tutorials, Tips & Tricks

Keywords:

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.