Skip to content

Instantly share code, notes, and snippets.

@aaronmader
Last active November 25, 2015 15:04
Show Gist options
  • Save aaronmader/ee714fb619041e0eb2d0 to your computer and use it in GitHub Desktop.
Save aaronmader/ee714fb619041e0eb2d0 to your computer and use it in GitHub Desktop.
A more robust version of the django 'cleanup' command, which requires less effort from the transaction handler of your database.
from datetime import datetime, timedelta
from django.core.management.base import NoArgsCommand
from django.contrib.sessions.models import Session
from django.db.transaction import commit
class Command(NoArgsCommand):
def handle_noargs(self, **options):
""" the conventional django cleanup method crashes when there's a few million records in django_sessions
so, do this without crashing.
"""
yesterday = datetime.today() - timedelta(days=2)
count = 0
oldest = Session.objects.all().order_by('expire_date')[:1]
if oldest:
oldest = oldest[0]
old_date = oldest.expire_date
while old_date < yesterday:
sessions = Session.objects.filter(expire_date__lt=old_date)
count += sessions.count()
if sessions.count() > 10000:
pks = sessions.values_list('session_key', flat=True)
for i in xrange(0,len(pks),10000):
chunk = pks[i:i+10000]
Session.objects.filter(session_key__in=chunk).delete()
commit()
else:
sessions.delete()
commit() # this is the key. circumventing django's desire to do this in a single transaction
old_date += timedelta(days=1)
print 'All done!'
print count, "records deleted"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment