Search code examples
djangodjango-modelssorl-thumbnail

sorl-thumbnail not deleting parent file or cache when entry is deleted


I'm not sure what I am doing wrong. But the uploaded file and it's related cache don't get deleted when the entry is deleted.

I have a photos model inline of a property model, with an FK from the photos model to the property model. I am using 'from sorl.thumbnail import ImageField' to replace the default Django models.ImageField.

In Django Admin, when I delete the entry of a photo, the entry is deleted but the files for that entry are not deleted. I am using Django's runserver for the development and I am not seeing any errors. From what I have read, these files should be removed if the entry is deleted, unless there is a reference to them yet. The only reference that I am seeing yet is in the thumbnail_kvstore table.

Anyone have any thoughts on what I am missing?


Solution

  • The ImageField from sorl.thumbnail should be an extension of django's FileField

    From the release notes of django 1.2.5:

    In earlier Django versions, when a model instance containing a FileField was deleted, FileField took it upon itself to also delete the file from the backend storage. This opened the door to several potentially serious data-loss scenarios, including rolled-back transactions and fields on different models referencing the same file. In Django 1.2.5, FileField will never delete files from the backend storage. If you need cleanup of orphaned files, you'll need to handle it yourself (for instance, with a custom management command that can be run manually or scheduled to run periodically via e.g. cron).