Soft Deletion Will Complicate Your Application
Here are the workarounds and an alternative
The data generated by a software application is valuable. This is also true for data that can be deleted from the application. Keeping deleted data around can help in tracking data history, trends, and relationships between data. On top of that, some parts of the application might be able to make use of deleted historical data.
As a result, a lot of software applications are designed in such a way that data is never actually deleted. One of the popular approaches for accomplishing this is Soft Deletion. Even though this approach looks simple, it often leads to complications. Here, we will look at what makes Soft Deletion complicated. We will also look at an alternative approach to Soft Deletion and it’s own pros and cons.
Supporting Soft Deletion
To support Soft Deletion, we simply add a new field to the schema. A record is flagged as deleted depending on the value of this field. For example, we might have the following schema for the users
table:
id: string
username: string
password: string
created_date: Date
deleted: boolean
In the above example, any user who has the deleted
field set to true
is considered as deleted.
The Pros of Soft Deletion
The most attractive advantage of soft deletion is that soft deleted data is easy to restore. Restoring data is as simple as changing the value of the flag field.
In addition, soft deletion is faster than hard deletion because UPDATE
operations in databases are generally faster than delete operations.
Soft Deletion can also be of huge help when debugging production data because of the flexibility that it provides. Analyzing production data is also easier when everything is in the same table or collection.
Gotchas of Soft Deletion
Though these advantages are attractive, soft deletion also introduces some complexities which can often be difficult to deal with.
First of all, every query in the application must include a condition that filters out deleted records. Failure to do this will…