Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
SachaMPS opened this issue Jan 13, 2017 · 9 comments
Closed
Labels

Comments

@SachaMPS
Copy link

SachaMPS commented Jan 13, 2017

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
Copy link
Author

SachaMPS commented Jan 16, 2017

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.

@jrief
Copy link
Collaborator

jrief commented Jan 16, 2017

Although, it doesn't fix this bug, you may manually apply this pending PR #912. It adds a management command to remove orphaned file.

@SachaMPS
Copy link
Author

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

@jrief
Copy link
Collaborator

jrief commented Jan 17, 2017

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

@yakky
Copy link
Member

yakky commented Jan 19, 2017

@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

@stale
Copy link

stale bot commented Jul 28, 2022

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 stale bot added the stale label Jul 28, 2022
@stale
Copy link

stale bot commented Aug 31, 2022

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

@stale stale bot closed this as completed Aug 31, 2022
@fsbraun fsbraun reopened this Nov 29, 2022
@stale stale bot removed the stale label Nov 29, 2022
@stale
Copy link

stale bot commented Feb 27, 2023

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 stale bot added the stale label Feb 27, 2023
@stale
Copy link

stale bot commented Mar 27, 2023

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

@stale stale bot closed this as completed Mar 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants