I am using 1.2.5 with a standard ImageField and using the built-in storage backend. Files upload fine but when I remove an entry from admin the actual file on the server does not delete.
-
Hm, actually it should. Check file permissions on your upload folder (change to 0777). – Torsten Engelbrecht Mar 21 '11 at 01:32
-
7Django removed the automatic deletion feature (for Googlers who see the above comment). – Mark Oct 29 '15 at 00:50
13 Answers
You can receive the pre_delete
or post_delete
signal (see @toto_tico's comment below) and call the delete() method on the FileField object, thus (in models.py):
class MyModel(models.Model):
file = models.FileField()
...
# Receive the pre_delete signal and delete the file associated with the model instance.
from django.db.models.signals import pre_delete
from django.dispatch.dispatcher import receiver
@receiver(pre_delete, sender=MyModel)
def mymodel_delete(sender, instance, **kwargs):
# Pass false so FileField doesn't save the model.
instance.file.delete(False)
-
11Be sure to add a check if `instance.file` field is not empty or it can (at least try) to delete the whole MEDIA_ROOT directory. This applies even to `ImageField(null=False)` fields. – Antony Hatchkins Feb 17 '13 at 09:58
-
51Thanks. In general, I would recommend to use the `post_delete` signal because it is safer in the case the delete fail for any reason. Then neither the model, neither the file would be deleted keeping the data consistent. Please correct me if my understanding of `post_delete` and `pre_delete` signals is wrong. – toto_tico Apr 08 '13 at 17:48
-
9Note that this does not delete the old file if you replace the file on a model instance – Mark Oct 29 '15 at 00:49
-
3This does not work for me in Django 1.8 outside admin. Is there a new way to do it? – caliph Dec 08 '15 at 10:29
-
It does work in Django 1.11. You should use **pre_delete** signal, ** post_delete** won't work. – Max Malysh Apr 17 '17 at 01:46
-
@jonalv Connect the signal in Django 1.9 to make it work: `request_finished.connect(mymodel_delete, sender=None, weak=True, dispatch_uid="mymodel_delete")`. Then the signature of `mymodel_delete` should be: `@receiver(post_delete, sender=Resource) def mymodel_delete(sender, **kwargs): instance = kwargs.get('instance')` – Tobias Ernst Oct 25 '17 at 11:03
-
Try django-cleanup
pip install django-cleanup
settings.py
INSTALLED_APPS = (
...
'django_cleanup.apps.CleanupConfig',
)

- 4,259
- 2
- 30
- 33
-
4After limited testing, I can confirm that this package still works for Django 1.10. – CoderGuy123 Aug 08 '16 at 02:01
-
1Nice. Works for me on Django 2.0. I'm also using S3 as my storage backend (http://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html) and it's happily deleting files from S3. – routeburn May 25 '18 at 16:24
-
1
Django 1.5 solution: I use post_delete for various reasons that are internal to my app.
from django.db.models.signals import post_delete
from django.dispatch import receiver
@receiver(post_delete, sender=Photo)
def photo_post_delete_handler(sender, **kwargs):
photo = kwargs['instance']
storage, path = photo.original_image.storage, photo.original_image.path
storage.delete(path)
I stuck this at the bottom of the models.py file.
the original_image
field is the ImageField
in my Photo
model.

- 1,021
- 1
- 9
- 7
-
8For anyone using Amazon S3 as a storage backend (via django-storages), this particular answer won't work. You'll get a `NotImplementedError: This backend doesn't support absolute paths.` You can easily fix this by passing the file field's name to `storage.delete()` instead of the file field's path. For example, replace the last two lines of this answer with `storage, name = photo.original_image.storage, photo.original_image.name` then `storage.delete(name)`. – Sean Azlin Aug 26 '14 at 02:42
-
2@Sean +1, I'm using that adjustment in 1.7 to delete thumbnails generated by django-imagekit on S3 via django-storages. https://docs.djangoproject.com/en/dev/ref/files/storage/#the-storage-class . Note: If you're simply using an ImageField (or FileField) you can use `mymodel.myimagefield.delete(save=False)` instead. https://docs.djangoproject.com/en/dev/ref/files/file/#additional-methods-on-files-attached-to-objects – Sep 11 '14 at 21:56
-
@user2616836 Can you use `mymodel.myimagefield.delete(save=False)` on `post_delete`? In other words, I can see that I can delete the file, but can you delete the file when a model that has the imagefield gets deleted? – eugene May 10 '15 at 12:11
-
1@eugene Yes you can, it works (I'm not sure why though). In `post_delete` you do `instance.myimagefield.delete(save=False)`, note the use of `instance`. – May 14 '15 at 20:10
This code runs well on Django 1.4 also with the Admin panel.
class ImageModel(models.Model):
image = ImageField(...)
def delete(self, *args, **kwargs):
# You have to prepare what you need before delete the model
storage, path = self.image.storage, self.image.path
# Delete the model before the file
super(ImageModel, self).delete(*args, **kwargs)
# Delete the file after the model
storage.delete(path)
It's important to get the storage and the path before delete the model or the latter will persist void also if deleted.

- 929
- 1
- 9
- 13
-
3This doesn't work for me (Django 1.5) and the Django 1.3 CHANGELOG states: "In Django 1.3, when a model is deleted the FileField’s delete() method won’t be called. If you need cleanup of orphaned files, you’ll need to handle it yourself (for instance, with a custom management command that can be run manually or scheduled to run periodically via e.g. cron)." – darrinm Jan 14 '13 at 00:51
-
4This solution is wrong! `delete` is not always called when a row is deleted, you must use signals. – lvella Dec 06 '13 at 03:19
You need to remove the actual file on both delete
and update
.
from django.db import models
class MyImageModel(models.Model):
image = models.ImageField(upload_to='images')
def remove_on_image_update(self):
try:
# is the object in the database yet?
obj = MyImageModel.objects.get(id=self.id)
except MyImageModel.DoesNotExist:
# object is not in db, nothing to worry about
return
# is the save due to an update of the actual image file?
if obj.image and self.image and obj.image != self.image:
# delete the old image file from the storage in favor of the new file
obj.image.delete()
def delete(self, *args, **kwargs):
# object is being removed from db, remove the file from storage first
self.image.delete()
return super(MyImageModel, self).delete(*args, **kwargs)
def save(self, *args, **kwargs):
# object is possibly being updated, if so, clean up.
self.remove_on_image_update()
return super(MyImageModel, self).save(*args, **kwargs)

- 17,692
- 14
- 63
- 66
You may consider using a pre_delete or post_delete signal:
https://docs.djangoproject.com/en/dev/topics/signals/
Of course, the same reasons that FileField automatic deletion was removed also apply here. If you delete a file that is referenced somewhere else you will have problems.
In my case this seemed appropriate because I had a dedicated File model to manage all of my files.
Note: For some reason post_delete doesn't seem to work right. The file got deleted, but the database record stayed, which is completely the opposite of what I would expect, even under error conditions. pre_delete works fine though.

- 8,203
- 5
- 49
- 57
-
3probably `post_delete` won't work, because `file_field.delete()` by default saves model to db, try `file_field.delete(False)` https://docs.djangoproject.com/en/1.3/ref/models/fields/#django.db.models.FieldFile.delete – Adam Jurczyk Mar 11 '12 at 22:16
Maybe it's a little late. But the easiest way for me is to use a post_save signal. Just to remember that signals are excecuted even during a QuerySet delete process, but the [model].delete() method is not excecuted during the QuerySet delete process, so it's not the best option to override it.
core/models.py:
from django.db import models
from django.db.models.signals import post_delete
from core.signals import delete_image_slide
SLIDE1_IMGS = 'slide1_imgs/'
class Slide1(models.Model):
title = models.CharField(max_length = 200)
description = models.CharField(max_length = 200)
image = models.ImageField(upload_to = SLIDE1_IMGS, null = True, blank = True)
video_embed = models.TextField(null = True, blank = True)
enabled = models.BooleanField(default = True)
"""---------------------------- SLIDE 1 -------------------------------------"""
post_delete.connect(delete_image_slide, Slide1)
"""--------------------------------------------------------------------------"""
core/signals.py
import os
def delete_image_slide(sender, **kwargs):
slide = kwargs.get('instance')
try:
os.remove(slide.image.path)
except:
pass

- 2,552
- 2
- 29
- 43
Using the post_delete is for sure the right way to go. Sometimes though things can go wrong, and files don't get deleted. There is of course the case that you have a bunch of old files that weren't deleted before post_delete was used. I created a function that deletes files for objects based on if the file the object references does not exist then delete object, if the file does not have an object, then also delete, also it can delete based on an "active" flag for an object.. Something I added to most of my models. You have to pass it the objects you want to check, the path to the objects files, the file field and a flag to delete inactive objects:
def cleanup_model_objects(m_objects, model_path, file_field='image', clear_inactive=False):
# PART 1 ------------------------- INVALID OBJECTS
#Creates photo_file list based on photo path, takes all files there
model_path_list = os.listdir(model_path)
#Gets photo image path for each photo object
model_files = list()
invalid_files = list()
valid_files = list()
for obj in m_objects:
exec("f = ntpath.basename(obj." + file_field + ".path)") # select the appropriate file/image field
model_files.append(f) # Checks for valid and invalid objects (using file path)
if f not in model_path_list:
invalid_files.append(f)
obj.delete()
else:
valid_files.append(f)
print "Total objects", len(model_files)
print "Valid objects:", len(valid_files)
print "Objects without file deleted:", len(invalid_files)
# PART 2 ------------------------- INVALID FILES
print "Files in model file path:", len(model_path_list)
#Checks for valid and invalid files
invalid_files = list()
valid_files = list()
for f in model_path_list:
if f not in model_files:
invalid_files.append(f)
else:
valid_files.append(f)
print "Valid files:", len(valid_files)
print "Files without model object to delete:", len(invalid_files)
for f in invalid_files:
os.unlink(os.path.join(model_path, f))
# PART 3 ------------------------- INACTIVE PHOTOS
if clear_inactive:
#inactive_photos = Photo.objects.filter(active=False)
inactive_objects = m_objects.filter(active=False)
print "Inactive Objects to Delete:", inactive_objects.count()
for obj in inactive_objects:
obj.delete()
print "Done cleaning model."
This is how you can use this:
photos = Photo.objects.all()
photos_path, tail = ntpath.split(photos[0].image.path) # Gets dir of photos path, this may be different for you
print "Photos -------------->"
cleanup_model_objects(photos, photos_path, file_field='image', clear_inactive=False) # image file is default

- 34,210
- 11
- 144
- 111
This functionality will be removed in Django 1.3 so I wouldn't rely on it.
You could override the delete
method of the model in question to delete the file before removing the entry from the database completely.
Edit:
Here is a quick example.
class MyModel(models.Model):
self.somefile = models.FileField(...)
def delete(self, *args, **kwargs):
somefile.delete()
super(MyModel, self).delete(*args, **kwargs)

- 1,151
- 13
- 28

- 3,473
- 3
- 25
- 34
-
Do you have an example of how to use that in a model in order to delete the file? I'm looking at the docs and see examples of how to remove the object from the database but do not see any implementations on file deletion. – narkeeso Mar 22 '11 at 22:59
-
2This method is wrong because it wont work for bulk delete (like admin's 'Delete selected' feature). For example `MyModel.objects.all()[0].delete()` will delete the file while `MyModel.objects.all().delete()` will not. Use signals. – Antony Hatchkins Feb 17 '13 at 09:24
make sure you write "self" before the file. so example above should be
def delete(self, *args, **kwargs):
self.somefile.delete()
super(MyModel, self).delete(*args, **kwargs)
I've forgotten the "self" before my file and that didn't work as it was looking in the global namespace.

- 1
If you already have number of unused files in your project and want to delete them, you can use django utility django-unused-media

- 446
- 4
- 7
Django 2.x Solution:
There's no need to install any packages! It's very easy to handle in Django 2. I've tried following solution using Django 2 and SFTP Storage (however I think it would work with any storages)
First write a Custom Manager. So if you want to be able to delete files of a model by using objects
methods, you must write and use a [Custom Manager][3] (for overriding delete()
method of objects
):
class CustomManager(models.Manager):
def delete(self):
for obj in self.get_queryset():
obj.delete()
Now you must delete image
before deleting deleting the model itself and for assigning the CustomManager
to the model, you must initial objects
inside your model:
class MyModel(models.Model):
image = models.ImageField(upload_to='/pictures/', blank=True)
objects = CustomManager() # add CustomManager to model
def delete(self, using=None, keep_parents=False):
objects = CustomManager() # just add this line of code inside of your model
def delete(self, using=None, keep_parents=False):
self.image.storage.delete(self.song.name)
super().delete()

- 1,465
- 12
- 31
I may have a special case since I am using the upload_to option on my file field with dynamic directory names but the solution I found was to use os.rmdir.
In models:
import os
...
class Some_Model(models.Model):
save_path = models.CharField(max_length=50)
...
def delete(self, *args,**kwargs):
os.rmdir(os.path.join(settings.MEDIA_ROOT, self.save_path)
super(Some_Model,self).delete(*args, **kwargs)

- 341
- 1
- 3
- 8
-
1This is a very bad idea. Not only will you remove an entire directory vs. a single file (potentially affecting other files), you will do so even if the actual object deletion fails. – tbm Feb 14 '17 at 01:34
-
Its not a bad idea if you were working on the problem I had ;) As I mentioned I had a unique use case where the model being deleted was a parent model. Children wrote files to the parent folder and so if you deleted the parent the desired behavior was that all files in the folder were deleted. Good point on the order of operations though. That didn't occur to me at the time. – carruthd Feb 15 '17 at 21:25
-
I would still prefer to remove the individual child files when a child is deleted; then if you need to you can remove the parent directory when it is empty. – tbm Feb 16 '17 at 01:16
-
That makes sense as you are taking out child objects but if the parent object is destroyed, stepping through the children one at a time seems tedious and unnecessary. Regardless, I see now that the answer I gave wasn't specific enough to the OP question. Thank you for the comments, you made me think about using a less blunt instrument going forward. – carruthd Feb 17 '17 at 12:21