A Django app that provides management commands for pruning unused media files.
uv add django-prune-media
Add it to your settings.py.
INSTALLED_APPS = [..., "prune_media", ...]
!!! warning
This application assumes you are not using the same storage for your static and media files.
It will look at whatever storage you have configured for `default`. If you are commingling them,
i.e. not using a separate "staticfiles" entry in STORAGES, this can result in false positives.
Usage:
To list or delete the media to be pruned:
$ python manage.py prune_media --help
Usage: django-admin prune_media [OPTIONS]
Remove unreferenced media files to save space.
╭─ Options ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --no-interaction --no-no-interaction Don't ask for confirmation before deleting. [default: no-no-interaction] │
│ --dry-run --no-dry-run Do a dry-run without deleting anything. [default: no-dry-run] │
│ --help Show this message and exit. │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Django ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --version Show program's version number and exit. │
│ --settings TEXT The Python path to a settings module, e.g. "myproject.settings.main". If this isn't provided, the │
│ DJANGO_SETTINGS_MODULE environment variable will be used. │
│ --pythonpath PATH A directory to add to the Python path, e.g. "/home/djangoprojects/myproject". [default: None] │
│ --traceback Raise on CommandError exceptions │
│ --no-color Don't colorize the command output. │
│ --force-color Force colorization of the command output. │
│ --skip-checks Skip system checks. │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Or to find empty directories:
$ python manage.py show_empty_media_dirs
Usage: django-admin show_empty_media_dirs [OPTIONS]
List empty media directories.
The storage API does not support deletion of directories but at least this way you know what can be removed.
╭─ Options ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --clean --no-clean Print paths only so they can be piped to other commands [default: no-clean] │
│ --help Show this message and exit. │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Django ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --version Show program's version number and exit. │
│ --settings TEXT The Python path to a settings module, e.g. "myproject.settings.main". If this isn't provided, the │
│ DJANGO_SETTINGS_MODULE environment variable will be used. │
│ --pythonpath PATH A directory to add to the Python path, e.g. "/home/djangoprojects/myproject". [default: None] │
│ --traceback Raise on CommandError exceptions │
│ --no-color Don't colorize the command output. │
│ --force-color Force colorization of the command output. │
│ --skip-checks Skip system checks. │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Most of the apps I've found operate from the assumption that you are using Django's FileSystemStorage
which is often not the case in production. If you're hosting your media files via object storage at a CDN, os.walk
is not going to work for you.
This application solely uses the Django Storage API, which means it works for custom backends like Django Storages too.
Django's Storage API doesn't support deleting anything other than files, so you can end up with empty directories. This is why the show_empty_media_dirs
command exists. When using the --clean
option you can pipe the results to a command that's appropriate to your setup.
🤷♂️
I made this because I didn't want to keep copying this between projects. I want to make it as useful as possible though so contributions, even if it's only a failing test case for me to fix, are very welcome!