django-filer
django-filer copied to clipboard
Orphaned files if file is replaced via Advanced -> Replace in admin
We noticed that we have lots of orphaned media files in filer_public/.
The root cause of this seems to be if a user replaces a file via Admin > Adnvanced > File: https://github.com/divio/django-filer/blob/develop/filer/admin/fileadmin.py#L55
Replacing a file won't delete the old file and won't delete all depending files like created thumbnails.
It seems that this is a very well known problem by django itself: http://stackoverflow.com/questions/4394194/replacing-a-django-image-doesnt-delete-original http://stackoverflow.com/questions/4787141/django-how-to-replace-overwrite-update-change-a-file-of-filefield https://code.djangoproject.com/ticket/11663#no1
So my question is: Can this be regarded as a bug and should it be fixed in django-filer?
I've tried one of the suggestions from stackoverflow... Adding this to the save method of filer.models.File seems to do the trick:
try:
this = File.objects.get(id=self.id)
if this.file != self.file:
this.file.delete(save=False)
except:
pass
As far as i understand it would make sense to add this behaviour. Either no files should be deleted at all or all files should be deleted on every action.
Although, it doesn't fix this bug, you may manually apply this pending PR https://github.com/divio/django-filer/pull/912. It adds a management command to remove orphaned file.
@jrief Thank you... actually I already stumbled over your PR and right now modifying it to delete also the thumbnails which are located in filer_public_thumbnails.
@SachaMPS if you have some time to work on this pull request, it would be great. I think @yakky 's proposal to add one command named filer_check
with these options
--orphans
: scan for orphans
--missing
: scan for missing
--delete-orphans
: delete orphans
--verbose=3
: output verbose string instead of raw filenames
would fulfill all of our requirements.
@SachaMPS while a bit brutal, I think that your solution is better than keeping orphaned files around. Do you mind to open a PR for your change? Also: any help on #912 is very much appreciated
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This will now be closed due to inactivity, but feel free to reopen it.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This will now be closed due to inactivity, but feel free to reopen it.