Skip to content

Soft Delete in Django Quickly

Posted in Django

Adrienne Domingus has a beautiful article on soft delete in Django, taking the custom model manager approach. (Article here). Let’s see another quick way to throw in soft delete in your Django application. It might not be the cleanest or best approach. However, it works well for my little setup.

What is Soft Delete?

Soft delete simply refers to giving the user an impression their item (or object) is deleted from the database, except only a flip was set on the object to hide it from usual view.

Therefore, if a user deletes their blog post, instead of truly, literally deleting the post from the database (which is truly, literally) irreversible, the developers simply sets a flag on the object, such as is_deleted=True, hiding the object from the usual running of the site.

Why am I even explaining what soft delete is?

A possible approach

Again, this is simply one way of going about it, which might not be the best approach for your use case. It is a free world; use what works best for you. In my case, I didn’t have to do lots of model level plumbings to get soft deletes working.

Here’s what I did.

models.py

Nothing fancy happening here. We’ve defined a new method on our model class, namely, soft_delete() which will simply flip add some values for us.

How do we put to use?

views.py

Using CBV’s DeleteView(), all we had to do is override the delete() method, and replace with our soft_delete() method defined earlier.

Pretty straightforward.

How to Query?

To keep all is_deletedflagged objects from our usual operations and query, be specific with your queries via filters.

Then in our views, we use the is_deletedfiltering to only select the none deleted objects.

Conclusion

Remember that, the above approach takes the opposite approach to what Adrienne’s article suggests. This approach maintains the existing functionality of Django’s model delete procedure (which is 100% permanent delete), while Adrienne’s replaces the default permanent delete of Django’s model with a soft delete version, then providing a hard_delete() method for the permanent deletion.

If your use case would mean flipping the soft delete feature app-wide in your Django project, a custom Model Manager might be useful.

For simple use cases, my approach might be useful and lean.

Hope you enjoyed this one, see you in the next one.

PS: Shouldn’t this (soft deletion thing) be a default feature in Django, by the way?