There are some similar questions on the topic, but they are not really helping me.
I want to implement a soft delete feature like on StackOverflow, where items are not really deleted, but just hidden. I am using a SQL database. Here are 3 options:
Add a is_deleted
boolean field.
is_deleted = 0
in every query.Add a deleted_date
date field. This is set to NULL
if it's not deleted.
For both of the above
deleted
column won't help when fetching non-deleted (the majority) of the rows. Full table scan is needed.Another option is to create a separate table to hold deleted items:
Is there a better option?
In my opinion, the best way forward, when thinking about scaling and eventual table/database sizes is your third option - a separate table for deleted items. Such a table can eventually be moved to a different database to support scaling.
I believe you have listed the three most common options. As you have seen, each has advantages and disadvantages. Personally, I like taking the longer view on things.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With