Giter VIP home page Giter VIP logo

django-unused-media's Introduction

Delete unused media files from Django project

build-status-image PyPI

Package provides management command cleanup_unused_media for Django applications.

You can remove all not used media files (files without references from any Django model with FileField or ImageField fields or their inheritances).

Installation

  1. Install django-unused-media:

    pip install django-unused-media
    

    Python 2.7, 3.6, 3.7, pypy are tested with tox.

    Django 1.8, 1.9, 1.10, 1.11, 2.0, 2.1, 2.2, 3.0 are tested with tox.

  2. Add django-unused-media to INSTALLED_APPS:

    INSTALLED_APPS = (
        ...
        'django_unused_media',
        ...
    )

Usage

To cleanup all unused media files, run management command:

./manage.py cleanup_unused_media

By default command is running in interactive mode. List of files which are going to be removed will be displayed for confirmation. User has to confirm the action.

Options

--noinput, --no-input

Non interactive mode. Command will remove files without any confirmation from the user. Useful for scripts.

./manage.py cleanup_unused_media --noinput

-e, --exclude

To avoid operating on particular files you can use exclude option.

  • * as any symbol is supported.
  • Can use multiple options in one command.

For example, to keep .gitignore and *.png files you can use:

./manage.py cleanup_unused_media -e *.gitignore -e *.png

Also you can exclude entire folder or some files in that folder (path should be relative to settings.MEDIA_ROOT):

./manage.py cleanup_unused_media -e path/to/dir/* -e path/to/dir/my*.doc

--minimum-file-age, seconds

Default = 60 (one minute)

Minimum file age to consider for cleanup. All files younger this age will be skipped.

--remove-empty-dirs

By default script keeps empty dirs in media folder. But with this option all empty directories will be removed after cleaning process automatically.

--dry-run

Dry run without any affect on your data

--verbosity {0,1,2}, -v {0,1,2}

Verbosity level.

  • 0 - silent
  • 1 - normal output (default)
  • 2 - verbose output (list individual files)

Tests

At first make sure that you are in virtualenv if you use it.

Install all dependencies:

make setup

To run tests:

make test

To run static analyser:

make flake8

License

MIT licence

django-unused-media's People

Contributors

akolpakov avatar athre0z avatar gagaro avatar georgy-komarov avatar mbaechtold avatar merofeev avatar turnrdev avatar urbanecm avatar yuriiz avatar ziima avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-unused-media's Issues

Changes from v.0.1.13 to v0.2.0

I started updating some dependencies and noticed that v0.2.0 is available. Congrats on the new release. It’s great to see that you are still maintaining the project.

I tried finding some information about the changes, but I couldn’t find any. Before pushing the upgrade to production, it would be useful to know what has changed. Would it be possible to have a changelog of sorts? (Personally, I have found https://keepachangelog.com/en/1.0.0/ to be a good guide.)

UnicodeDecodeError if a filename includes non-ascii characters

The code seems to assume ascii file name characters, even though with Django media, the filenames typically originate with users who upload files.

This error is with Django 1.8, Python 2.7, and macOS.

The error is in stdout.write, but there may be other issues as the u'' convention is not used in filename-related code. (from future import unicode_literals might be a good idea).

File "python2.7/site-packages/django_unused_media/management/commands/cleanup_unused_media.py", line 43, in handle
    self.stdout.write(f)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xcc in position 23: ordinal not in range(128)

Default action should be uppercase

Action triggered by hitting Enter without input should be uppercase, which should show uppercase N instead of Y.

As is:

$ .../manage.py  cleanup_unused_media
These files will be deleted:
screenshots/Screen_Shot_2017-01-16_at_10.56.29.png
Are you sure you want to remove 1 unused files? (Y/n)

Should be:

$ .../manage.py  cleanup_unused_media
These files will be deleted:
screenshots/Screen_Shot_2017-01-16_at_10.56.29.png
Are you sure you want to remove 1 unused files? (y/N)

Deletes used files.

Hi,

The command cleanup_unused_media just figures out used files as deletable an deletes them.

Thank You.

My environment:
Django 1.11.13
django-unused-media 0.1.11
Windows 7
python 3.4

Add dry-run mode

It would be very helpful if the command would have a dry-run mode.

missing pip version 0.1.13

I am having troubles installing version 0.1.13.
If I do pip install django-unused-media I get version 0.1.12 installed.
If I specify the version explictly, I get the following error:

$ pip install django-unused-media==0.1.13
Collecting django-unused-media==0.1.13
  Could not find a version that satisfies the requirement django-unused-media==0.1.13 (from versions: 0.1.0, 0.1.1, 0.1.2, 0.1.3, 0.1.5, 0.1.6, 0.1.7, 0.1.8, 0.1.9, 0.1.10, 0.1.11, 0.1.12)
No matching distribution found for django-unused-media==0.1.13

Add support of list of exclusion in settings.py

First, thank you for this nice package!
I have some directories and files in media/ accessed from django-filebrowser (file cloud in admin). My shell gives me command history, so I always add as many --exclude arguments as I need. But my project will be used by other people, who can miss some files and some logic will be broken. I believe a good solution is to have a list in settings.py containing strings - elements of --exclude.

Add support for django-stdimage variations

Hiyo..

noticed this removed thumbnails and such for my projects.. So I decided to extend this to support the variations field that django-stdimage uses..

#5

Feel free to merge

Cheers!

Conflicts with constance settings

Just noticed if I used a file setting in constance, django-unused-media will delete it. The process to use a file setting is described here.

Wondering if there's a workaround, or worth patching? (I'm happy to write the patch if you'd be willing to accept it!)

Cheers,

David

Suggestion: only remove files older than <some period of time>

With large databases and many files there can be a delay between getting the used files from the database and all the files from disk. Inbetween a file may have been uploaded, which will then be deleted by the script. While this is an edge case and not very likely to happen, I would suggest adding an option (or making it default) to only remove files that have not been modified for some length of time, for example a couple of minutes or an hour.

If you like this idea I'll be happy to prepare a pull request adding this functionality, but before I do that I am wondering what your thoughts are.

Support ignoring recent files

There's a potential race between an application writing a new file and updating the database, such that a media file that has not yet been recorded in the database is treated as unused:

  1. Application writes file to media storage
  2. Scan finds file in storage
  3. Scan doesn't find file in database
  4. Application writes model referring to file to the database

The window between 1 and 4 will usually be short, but a very fast scan for unused files or transactionality-related delays in the database could make this more likely to happen.


Supporting an option to ignore files that were created within some time period can make it arbitrarily difficult to cause this problem. For instance if any file that's less than one day old is ignored, it's virtually impossible for this to happen because it's difficult to imagine a reasonable application taking a day to complete a database write. However, making this a user-accessible option would allow users to decide an appropriate interval for themselves in making a tradeoff between prompt pruning of unused files and potential data loss.

I'm unsure how feasible this is to do in a portable way, though- I haven't looked into it in any detail.

Support for AWS S3 backend?

I just gave a try to django-unused-media today.
It works perfectly out of the box with my media files hosted on my own workstation.

But, in production, I use AWS S3 to store media and static files into a bucket.
And I got this exception : NotImplementedError: This backend doesn't support absolute paths.

Is AWS S3 supported?
Or I am missing something?

Thanks!

'Options' object has no attribute 'get_fields' error encountered

hi

i recently installed this on a django project i'm working on to do cleanups of my unused media files. I ran the cleanup_unused_media command with my virtualenv enabled and i got the following result. I tried it with

python manage.py cleanup_unused_media

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/dev/.virtualenvs/TestProject/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/dev/.virtualenvs/TestProject/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/dev/.virtualenvs/TestProject/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/dev/.virtualenvs/TestProject/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/dev/.virtualenvs/TestProject/local/lib/python2.7/site-packages/django_unused_media/management/commands/cleanup_unused_media.py", line 32, in handle
    unused_media = get_unused_media(options.get('exclude') or [])
  File "/home/dev/.virtualenvs/TestProject/local/lib/python2.7/site-packages/django_unused_media/cleanup.py", line 91, in get_unused_media
    used_media = get_used_media()
  File "/home/dev/.virtualenvs/TestProject/local/lib/python2.7/site-packages/django_unused_media/cleanup.py", line 43, in get_used_media
    for f in _get_file_fields():
  File "/home/dev/.virtualenvs/TestProject/local/lib/python2.7/site-packages/django_unused_media/cleanup.py", line 29, in _get_file_fields
    for f in m._meta.get_fields():
AttributeError: 'Options' object has no attribute 'get_fields'

i tried running it with the option --noinput enabled but i got the following error

python manage.py cleanup_unused_media --noinput
Usage: manage.py cleanup_unused_media [options] 

Clean unused media files which have no reference in models

manage.py: error: no such option: --noinput

Is there a missing option for this which is why it's not running? I've installed django_unused_media in my INSTALLED_APPS as well. Thanks.

Edit: Forgot to note that i'm using Django 1.7.

Thanks a lot!

I came across this project after some quick googling (via this question), and it solved my problem.

Just want to say that I really appreciate you sharing and maintaining it ❤️

Missing tag 0.1.13

Although 0.1.13 have been released to pypi, corresponding tag is missing on github.

--noinput does not work as expected

Hi,

when adding --noinput to management command it still asks if we want to remove following items:

$ .../manage.py  cleanup_unused_media --noinput
These files will be deleted:
screenshots/Screen_Shot_2017-01-16_at_10.56.29.png
Are you sure you want to remove 1 unused files? (Y/n)

Btw, i'm using Django 1.10.5 and Python 2.7.5.

Usage of `--minimum-file-age` parameter leads to crash

When trying to override default values of --minimum-file-age e.g

python3 manage.py cleanup_unused_media --dry-run  --minimum-file-age 42

The tool fails with the error

  File "/usr/local/lib/python3.6/site-packages/django_unused_media/cleanup.py", line 59, in get_all_media
    if file_age < minimum_file_age:
TypeError: '<' not supported between instances of 'float' and 'list'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.