Question

When I delete a model's instance that has a FileField, the actual file is left in my MEDIA_ROOT folder. To counter this, I'm listening for the post_delete signal, and doing this:

def delete_actual_file(sender,**kwargs):
  import os
  instance = kwargs.get("instance")
  os.unlink(instance.file.path)

post_delete.connect(delete_actual_file,sender=ModelWithFileField)

It seems to work ok, but I'm not sure if it's best practice to do it like this. Any thoughts?

Was it helpful?

Solution

Which version of Django are you using? The behaviour changed in Django 1.3. The docs you might want are here and you can hook that into your signal, but it'd have to become a pre-delete action, I imagine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top