Skip to content

About

No description, website, or topics provided.

Resources

Stars

3 stars

Watchers

1 watching

Forks

Repository files navigation

django-prune-uploads - Prune and maintain file uploads

Installation

For filesystem storage (default):

pip install django-prune-uploads

For S3 storage backends:

pip install django-prune-uploads[s3]

Then add prune_uploads to your INSTALLED_APPS.

Usage

  1. Run ./manage.py prune_uploads and have a look at the output (does not change anything in the database or the file system!)
  2. Run ./manage.py prune_uploads -v2 for a potentially much more verbose report.
  3. Run ./manage.py prune_uploads --help to learn about the available options for actually changing and/or removing files and records.

Finding orphans

A file in a storage is an orphan if no file field of any model refers to it. References are read through each model's _base_manager, so rows hidden by a filtering default manager still count.

--delete-orphans deletes them. It refuses to run if the database references no file at all (e.g. an empty or the wrong database). A file which fails to delete is reported and the run continues; a summary (deleted, failed, not attempted) is printed at the end, also when the run is interrupted. Names the storage would resolve to another file (e.g. a//b) are reported and never deleted.

Minimum age

Code often stores a file before saving the row which refers to it (uploads, imports). Unreferenced files modified within the last --min-age-hours (default: 24) are therefore kept and reported separately, as are files whose age the storage cannot tell. --min-age-hours=0 disables the check.

Prefixes

--prefix limits the command to files below the given prefix; it may be given several times:

./manage.py prune_uploads --prefix=documents/ --prefix=images/

Nothing outside the prefixes is listed, deleted or checked for missing files. Prefixes are compared as strings, so add the trailing slash to only match a directory (image also matches images-old/).

django-imagefield

Directories named __something__ are never listed. With --imagefield-processed the processed images of django-imagefield in __processed__/ are checked as well: a processed image of a referenced image of an imagefield ImageField is kept, all others are orphans. When using --prefix, add --prefix=__processed__/ too.

Project-specific checks

To add your own checks (e.g. refuse to delete files in the production bucket when running against a local database), add a management command to your project which subclasses the command:

from django.core.management.base import CommandError
from prune_uploads.management.commands.prune_uploads import (
    Command as PruneUploadsCommand,
)


class Command(PruneUploadsCommand):
    def handle(self, **options):
        if options["delete_orphans"] and running_against_local_database():
            raise CommandError("Not deleting from here.")
        super().handle(**options)

Storage backends

S3 storage

S3 storages of django-storages and django-s3-storage are detected by their attributes (bucket name, key prefix and boto3 connection) and listed with list_objects_v2 directly, which is much faster than going through the storage API and also provides the modification times without additional requests. The key prefix (location resp. AWS_S3_KEY_PREFIX) is respected.

Filesystem storage

FileSystemStorage (and subclasses) are listed with os.walk().

Other storages

All other storages are listed using Storage.listdir() and Storage.get_modified_time().

About

No description, website, or topics provided.

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages