DEV Community

Cover image for Mastering Soft Delete
Joshua Hassan
Joshua Hassan

Posted on

Mastering Soft Delete

Have you ever wondered what happens to that cute puppy picture you accidentally deleted from your phone? Vanished to digital oblivion? Not quite! There's a secret world in databases where deleted data isn't gone, it's just playing hide-and-seek. This magical trick is called soft delete, and it's a superhero for developers like you and me.

Imagine this: You're building a shopping app, and a customer accidentally removes their dream sneakers from their cart. Panic sets in, right? But not if you have soft delete! Instead of disappearing forever, those sneakers simply move to a special corner of the database, marked as "deleted" but still ready to be rescued. This gives you the chance to be the digital hero, swooping in to restore the order and save the day (and the customer's shopping spree).

Why is soft delete so awesome? Think of it as a safety net for your database:

  • Accidentally erased your grandma's recipe in your recipe app? No worries, soft delete brings it back with a snap.
  • Need to track who deleted what and when? Soft delete keeps an audit log, like a digital detective, making it easy to solve database mysteries.
  • Worried about deleting something connected to other things? Soft delete handles relationships like a pro, ensuring everything stays balanced and happy.

But hey, just like any power, soft delete needs training. Here's how you can wield it like a coding wizard in Django:

1. Building the Secret Hideout:

Create a special "SoftDeleteModel" that has a magic field called deleted_at. This field acts like a timer, marking the moment something went into hiding. Then, all your other models can inherit this power, making them soft-delete masters!

from django.db import models
from django.utils import timezone

class SoftDeleteManager(models.Manager):
    def with_deleted(self):
        return super().get_queryset().all()

class SoftDeleteModel(models.Model):
    deleted_at = models.DateTimeField(blank=True, null=True)

    objects = SoftDeleteManager()

    class Meta:
        abstract = True

    def soft_delete(self):
        self.deleted_at = timezone.now()
        self.save()

    def restore(self):
        self.deleted_at = None
        self.save()

class YourModel(SoftDeleteModel):
    # other fields go here

Enter fullscreen mode Exit fullscreen mode

2. Finding the Hidden Treasures:

By default, only the living see the living. To see the ghosts in your database, you need to say the magic words: objects.with_deleted(). This reveals the hidden treasures, letting you see everything, even the deleted stuff.

# To retrieve deleted and non-deleted records
YourModel.objects.with_deleted().all()

# To retrieve only non-deleted records
YourModel.objects.all()
Enter fullscreen mode Exit fullscreen mode

3. Bringing Back the Lost Souls:

Want to bring a deleted item back to life? Easy! Just set the deleted_at timer back to zero, and poof! It's back in the land of the living, ready to be used again.

instance = YourModel.objects.get(pk=1)
instance.restore()
Enter fullscreen mode Exit fullscreen mode

Remember: Soft delete is a powerful tool, but with great power comes great responsibility. Make sure you have good reasons for keeping deleted things around, and don't let your database become a haunted house of forgotten data.

Now, go forth and conquer the world of soft delete! Remember, it's not just about saving data, it's about saving the day (and maybe preventing a few tears along the way). And who knows, maybe you'll even inspire other newbie developers to embrace the power of the disappearing act!

Bonus Tips for the Superheroes of Tomorrow:

  • Check out libraries like django-safedelete, django-soft-delete and django-paranoid for extra soft-delete superpowers.
  • Remember, even soft-deleted data takes up space, so have a plan for cleaning up the digital graveyard every now and then.
  • And most importantly, have fun and experiment! Soft delete is a playground for your coding creativity, so go wild and build amazing things!

Top comments (0)