maintenance.py 642 B

123456789101112131415161718
  1. from .models import TransactionGroup
  2. def cleanup_old_transaction_events():
  3. """Delete older events and associated data"""
  4. # Delete ~1k empty transaction groups at a time until less than 1k remain then delete the rest. Avoids memory overload.
  5. queryset = TransactionGroup.objects.filter(transactionevent=None).order_by("id")
  6. while True:
  7. try:
  8. empty_group_delimiter = queryset.values_list("id", flat=True)[
  9. 1000:1001
  10. ].get()
  11. queryset.filter(id__lte=empty_group_delimiter).delete()
  12. except TransactionGroup.DoesNotExist:
  13. break
  14. queryset.delete()