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

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.7.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_prune_media-0.1.7-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_prune_media-0.1.7.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.19

File hashes

Hashes for django_prune_media-0.1.7.tar.gz
Algorithm Hash digest
SHA256 a57abb2566df5c996236885064a1b1fda111f5d96e248671134dfbddffbba2fc
MD5 f3488f83494954d8add40d744bd6e6d9
BLAKE2b-256 6dc36ba2e266502368bcc9aeba8552d8201e7522de54d9f8c07564f4ad0816b7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_prune_media-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 7f691b48aaa32ca1bdb191e69160ff1fac58b23d622037231e50ebe44e519be0
MD5 66399ce2a2039b8c218db908873b6f83
BLAKE2b-256 8c19d64b3be5f7c6d9b83581680ab7e6d307a56374b9ec83009923a67060089f

See more details on using hashes here.

Supported by

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