Remove all thumbnails generated with easy-thumbnails Django App
Asked Answered
S

2

9

I am using easy-thumbnails in my Django 1.5 project to generate thumbnail images.

I have been using several different sizes for thumbnails for testing, but now I would like to clear all thumbnails from my filesystem and from the easy-thumbnails database entries. Over time I created several different sizes of many images and I would like to remove those now.

My intention is to start with a clean slate and to remove all thumbnail images. I could not find out how to do that.

Shaveling answered 25/4, 2013 at 17:47 Comment(0)
D
10

Just had the same problem.

Given:

class MyModel(Model):
    image = ThumbnailerImageField()

You can delete all thumbnails with:

for m in MyModel.objects.all():
    m.image.delete_thumbnails()

If you instead have:

class MyModel(Model):
    image = ImageField()

Then you should use:

from easy_thumbnails.files import get_thumbnailer

for m in MyModel.objects.all():
    thumbnailer = get_thumbnailer(m.image)
    thumbnailer.delete_thumbnails()
Dachy answered 27/4, 2014 at 1:6 Comment(2)
works for me, but import should be: from easy_thumbnails.files import get_thumbnailer (missing s in easy_thumbnails in the code above)Bertsche
Hey guys. Actually, this solution does not work for me. Is it correct now?Underprop
R
6

I have created a Picture model in which I added a method, as follows

from easy_thumbnails.models import Source, Thumbnail
def clean_thumbnail(self):
    if self.image:
        sources = Source.objects.filter(name=self.image.name)
        if sources.exists():
            for thumb in Thumbnail.objects.filter(source=sources[0]):
                try:
                    os.remove(os.path.join(settings.MEDIA_ROOT, thumb.name))
                    thumb.delete()
                except Exception, e:
                    logger.warning(e)

And it works like a charm.

Relator answered 5/8, 2013 at 22:46 Comment(1)
Could have used thumbnail_default_storage to delete the file.Disjointed

© 2022 - 2024 — McMap. All rights reserved.