Skip to content

Commit

Permalink
use batching in email queryset and attachment queryset (#449)
Browse files Browse the repository at this point in the history
* use batch in email queryset and attachment queryset

* not batching attachament

* update

* fix error

* fix

* revert back to batch attachments delete in different flow
  • Loading branch information
marsha97 authored Aug 8, 2023
1 parent cc0182f commit 95d68c1
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions post_office/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,29 @@ def cleanup_expired_mails(cutoff_date, delete_attachments=True, batch_size=1000)
Optionally also delete pending attachments.
Return the number of deleted emails and attachments.
"""
expired_emails_ids = Email.objects.filter(created__lt=cutoff_date).values_list('id', flat=True)
email_id_batches = split_emails(expired_emails_ids, batch_size)
total_deleted_emails = 0

for email_ids in email_id_batches:
# Delete email and incr total_deleted_emails counter

while True:
email_ids = Email.objects.filter(created__lt=cutoff_date).values_list('id', flat=True)[:batch_size]
if not email_ids:
break

_, deleted_data = Email.objects.filter(id__in=email_ids).delete()
if deleted_data:
total_deleted_emails += deleted_data['post_office.Email']

attachments_count = 0
if delete_attachments:
attachments = Attachment.objects.filter(emails=None)
for attachment in attachments:
# Delete the actual file
attachment.file.delete()
attachments_count, _ = attachments.delete()
else:
attachments_count = 0
while True:
attachments = Attachment.objects.filter(emails=None)[:batch_size]
if not attachments:
break
attachment_ids = set()
for attachment in attachments:
# Delete the actual file
attachment.file.delete()
attachment_ids.add(attachment.id)
deleted_count, _ = Attachment.objects.filter(id__in=attachment_ids).delete()
attachments_count += deleted_count

return total_deleted_emails, attachments_count

0 comments on commit 95d68c1

Please sign in to comment.