Skip to content

Commit 9e2e7f8

Browse files
Improvement cleanup expired mails (#386)
* Improvement delete expired mails mechanism * update * update * Added `--batch-size` argument * Updated default batch size to 1000 * Removed `.only('id')` from query * Updated cleanup batching function * Updated variable names Co-authored-by: Ikhtiary Rizky <[email protected]>
1 parent 4584ba8 commit 9e2e7f8

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

post_office/management/commands/cleanup_mail.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ def add_arguments(self, parser):
1717
parser.add_argument('-da', '--delete-attachments', action='store_true',
1818
help="Delete orphaned attachments.")
1919

20-
def handle(self, verbosity, days, delete_attachments, **options):
20+
parser.add_argument('-b', '--batch-size', type=int, default=1000, help="Batch size for cleanup.")
21+
22+
def handle(self, verbosity, days, delete_attachments, batch_size, **options):
2123
# Delete mails and their related logs and queued created before X days
2224
cutoff_date = now() - datetime.timedelta(days)
23-
num_emails, num_attachments = cleanup_expired_mails(cutoff_date, delete_attachments)
25+
num_emails, num_attachments = cleanup_expired_mails(cutoff_date, delete_attachments, batch_size)
2426
msg = "Deleted {0} mails created before {1} and {2} attachments."
2527
self.stdout.write(msg.format(num_emails, cutoff_date, num_attachments))

post_office/utils.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ def split_emails(emails, split_count=1):
5858
if list(emails):
5959
return [emails[i::split_count] for i in range(split_count)]
6060

61+
return []
62+
6163

6264
def create_attachments(attachment_files):
6365
"""
@@ -138,14 +140,21 @@ def parse_emails(emails):
138140
return emails
139141

140142

141-
def cleanup_expired_mails(cutoff_date, delete_attachments=True):
143+
def cleanup_expired_mails(cutoff_date, delete_attachments=True, batch_size=1000):
142144
"""
143145
Delete all emails before the given cutoff date.
144146
Optionally also delete pending attachments.
145147
Return the number of deleted emails and attachments.
146148
"""
147-
expired_emails = Email.objects.only('id').filter(created__lt=cutoff_date)
148-
emails_count, _ = expired_emails.delete()
149+
expired_emails_ids = Email.objects.filter(created__lt=cutoff_date).values_list('id', flat=True)
150+
email_id_batches = split_emails(expired_emails_ids, batch_size)
151+
total_deleted_emails = 0
152+
153+
for email_ids in email_id_batches:
154+
# Delete email and incr total_deleted_emails counter
155+
_, deleted_data = Email.objects.filter(id__in=email_ids).delete()
156+
if deleted_data:
157+
total_deleted_emails += deleted_data['post_office.Email']
149158

150159
if delete_attachments:
151160
attachments = Attachment.objects.filter(emails=None)
@@ -156,4 +165,4 @@ def cleanup_expired_mails(cutoff_date, delete_attachments=True):
156165
else:
157166
attachments_count = 0
158167

159-
return emails_count, attachments_count
168+
return total_deleted_emails, attachments_count

0 commit comments

Comments
 (0)