Skip to main content

Adds tools for pruning media files from your django project

Project description

Django Prune Media

A Django app that provides management commands for pruning unused media files.

PyPI PyPI - Python Version PyPI - Versions from Framework Classifiers Black code style Pre-commit License uv Ruff Security: bandit Checked with pyright Semantic Versions Test results Coverage Status Documentation

Installation

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.                                                                                    │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

FAQ

Why another app for this?

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 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.

What are the limitations?

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.

Should I use this?

🤷‍♂️

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!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_prune_media-0.1.2.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

django_prune_media-0.1.2-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file django_prune_media-0.1.2.tar.gz.

File metadata

File hashes

Hashes for django_prune_media-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7375bf3edde57ace0a074efcdec75050129872044452afee545d5a8dc0e5ea8b
MD5 3a1a18bdbf661a244831b463c1bf6eac
BLAKE2b-256 c7187b6c17ecaf9f1135c4c0d6a91c8aae8e4af40122b1ef604d7f143e848888

See more details on using hashes here.

File details

Details for the file django_prune_media-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_prune_media-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d35ba8a83571c9fba5fb1997bfd6938d21c55a2095a5950bde951510a16cda18
MD5 317fb022a0144ea1688a7128be93dbbe
BLAKE2b-256 8f73f171d0bcf849048fee45e36a662fc1a03bb5c3a68bfb4e58e1351a825437

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page