Skip to main content

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 --help
                                                                                
 Usage: django-admin show_empty_media_dirs [OPTIONS]                            
                                                                                
 List empty media directories for review or to pipe to another command.         
                                                                                
╭─ 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 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.

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!

Metadata

Release files for django-prune-media 0.1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-prune-media 0.1.7
File Size Uploaded Provenance Actions
django_prune_media-0.1.7.tar.gz 18.9 kB Without provenance Details for django_prune_media-0.1.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-prune-media 0.1.7
File Interpreter ABI Platform Actions
django_prune_media-0.1.7-py3-none-any.whl Python 3 none any Details for django_prune_media-0.1.7-py3-none-any.whl

Total release size: 29.0 kB

Release files / django_prune_media-0.1.7.tar.gz

Download URL django_prune_media-0.1.7.tar.gz
Size 18.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a57abb2566df5c996236885064a1b1fda111f5d96e248671134dfbddffbba2fc
BLAKE2b-256 checksum
How to use checksums
6dc36ba2e266502368bcc9aeba8552d8201e7522de54d9f8c07564f4ad0816b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.19

Release files / django_prune_media-0.1.7-py3-none-any.whl

Download URL django_prune_media-0.1.7-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7f691b48aaa32ca1bdb191e69160ff1fac58b23d622037231e50ebe44e519be0
BLAKE2b-256 checksum
How to use checksums
8c19d64b3be5f7c6d9b83581680ab7e6d307a56374b9ec83009923a67060089f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.19

Security for django-prune-media 0.1.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page