Skip to content

Instantly share code, notes, and snippets.

Created March 31, 2017 19:21
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Django management command to delete old celery tasks from database using batches of adaptive size
import time
from import BaseCommand
from django.db.models import Max, Min
from django.db.utils import ProgrammingError
from djcelery.models import TaskMeta
class Command(BaseCommand):
help = 'Delete old celery tasks'
def handle(self, *args, **options):
result = TaskMeta.objects.aggregate(Max('id'), Min('id'))
id_min, id_max = result['id__min'], result['id__max']
"Total: %r, ranging from %r to %r"
% (TaskMeta.objects.count(), id_min, id_max)
if id_min is None or id_max is None:
self.stdout.write("Nothing to do, exiting")
id_max -= 100
self.stdout.write("Decreased finish down to %d" % id_max)
step = 100
start = id_min
while start < id_max:
finish = start + step
deleted, _ = TaskMeta.objects.filter(id__gte=start, id__lt=finish).delete()
"For range %d-%d deleted %d objects"
% (start, finish, deleted)
step = int(step * 1.05)
start = finish
except ProgrammingError as exc:
step = max(10, step / 2)
Copy link

invoke with cleanup_tasks

Copy link

I made a short write-up about this management command at

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment