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?
If the key is numeric, I handle a "soft-delete" by negating the key. (Of course, won't work for identity keys). You don't need to change your code at all, and can easily restore the record by multiplying by -1.
Just another approach to give some thought to... If the key is alphanumeric, you can do something similar by prepending a unique "marker" characters. Since deleted records will all begin with this marker, then will end up off by themselves in the index.