Delete All Thumbnails Created Using Simple Django App Thumbnails

I use easy-thumbnails in my Django 1.5 project to create thumbnails.

I use several different sizes for thumbnails for testing, but now I would like to clear all thumbnails from my file system and from the easy-thumbnails database entries. Over time, I created several different sizes of many images, and I would like to delete them now.

I intend to start from scratch and delete all thumbnails. I could not figure out how to do this.

+6
source share
2 answers

You 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 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() 
+5
source

I created an image model in which I added a method as shown below.

 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.

+3
source

Source: https://habr.com/ru/post/943639/


All Articles