django-filer icon indicating copy to clipboard operation
django-filer copied to clipboard

Orphaned files if file is replaced via Advanced -> Replace in admin

Open SachaMPS opened this issue 8 years ago • 6 comments

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?

SachaMPS avatar Jan 13 '17 17:01 SachaMPS

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.

SachaMPS avatar Jan 16 '17 10:01 SachaMPS

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 avatar Jan 16 '17 17:01 jrief

@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 avatar Jan 16 '17 17:01 SachaMPS

@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.

jrief avatar Jan 17 '17 09:01 jrief

@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

yakky avatar Jan 19 '17 22:01 yakky

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.

stale[bot] avatar Jul 28 '22 22:07 stale[bot]

This will now be closed due to inactivity, but feel free to reopen it.

stale[bot] avatar Aug 31 '22 00:08 stale[bot]

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.

stale[bot] avatar Feb 27 '23 20:02 stale[bot]

This will now be closed due to inactivity, but feel free to reopen it.

stale[bot] avatar Mar 27 '23 21:03 stale[bot]