Delete orphaned replaced files
|Reported by:||Chris Beaven||Owned by:||Chris Beaven|
|Cc:||Johannes Bornhold||Triage Stage:||Design decision needed|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
When using a model instance with a
FileField (or subclass), you can update the image by setting a new file as an instance matching the field. This leaves the potential of an orphaned file.
A common case is that you have a
ModelForm (bound to an existing instance) for which you use to upload a new file:
- The form is saved in your view which overwrites the linked
FieldFilewith a new one (which will be based on the data from the
- Now there is no reference to the old file, so even custom storage can't touch it (unless you use an
upload_tocallable which names it the same and your custom storage is changed to dangerously blindly overwrite files with the same name).
Note, the delete orphan logic already exists -
FileField attaches a post_delete signal to it's Model class which does this.
This is obviously a change in functionality. To preserve backwards compatibility, I recommend that this is a new argument on
FileField (off by default).
Change History (25)
comment:1 Changed 7 years ago by
|Owner:||changed from nobody to Chris Beaven|
|Patch needs improvement:||unset|
|Status:||new → assigned|
comment:16 Changed 5 years ago by