Skip to content

Instantly share code, notes, and snippets.

@iksi
Created January 7, 2020 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iksi/ec83f8e523bee73354f5292c1068831c to your computer and use it in GitHub Desktop.
Save iksi/ec83f8e523bee73354f5292c1068831c to your computer and use it in GitHub Desktop.
Django management command to delete Wagtail’s image renditions
import os, shutil
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from wagtail.images.models import Rendition
class Command(BaseCommand):
def handle(self, *args, **options):
renditions = Rendition.objects.all()
if renditions:
images_root = os.path.join(settings.MEDIA_ROOT, 'images')
message = ['\n']
message.append('This will REMOVE ALL IMAGE RENDITION FILES in {}!\n'.format(images_root))
message.append(
'Are you sure you want to do this?\n\n'
"Type 'yes' to continue, or 'no' to cancel: "
)
if input(''.join(message)) != 'yes':
raise CommandError("Remove renditions cancelled.")
else:
# doe het
renditions.delete()
shutil.rmtree(images_root)
self.stdout.write('Done')
else:
self.stdout.write('There are no image renditions')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment