Manage Django Media Files automatically
How to auto delete media files in django
Django or Django Rest Framework never deletes user uploaded files (media) when you delete a record from model .
Sometimes it results in consuming more storage or production on servers .To overcome this flaw i will be using a package called django-cleanup .
Lets set it up under 2 minutes !
Installation
pip install django-cleanup
Configuration
just add django_cleanup to INSTALLED_APPS in settings.py
INSTALLED_APPS = ( ...
'django_cleanup.apps.CleanupConfig',
)
THAT’S IT ! We Done it
BONUS NOTE !
If you want to exclude any model to not delete files when you delete a record just add below code to required model in models.py
from django_cleanup import cleanup @cleanup.ignore
class MyModel(models.Model):
image = models.FileField()
If you want to read more about this package read Django Cleanup official open source Github Repo.