diff --git a/apimail/management/commands/delete_orphaned_attachment_files.py b/apimail/management/commands/delete_orphaned_attachment_files.py index e1b7feef7c3fb5c8b4b5db0b104717239dc63e91..146ada44d126b75712155cfa7aab28279e863c16 100644 --- a/apimail/management/commands/delete_orphaned_attachment_files.py +++ b/apimail/management/commands/delete_orphaned_attachment_files.py @@ -18,22 +18,14 @@ class Command(BaseCommand): for att in sm.attachment_files.all()] uuids_in_use = set(uuids_in_cm + uuids_in_sm) - print(uuids_in_use) - orphaned_att = AttachmentFile.objects.exclude(uuid__in=uuids_in_use) - print(orphaned_att) - for orphan_att in orphaned_att: # We double-check that we're not deleting any used attachment # since the 'exclude' logic above is risky # (any mistake in uuids_in_use would lead to unwanted deletion) - try: - ComposedMessage.objects.filter(attachment_files__uuid=orphan_att.uuid).first() - except ComposedMessage.DoesNotExist: - try: - StoredMessage.objects.filter(attachment_files__uuid=orphan_att.uuid).first() - except StoredMessage.DoesNotExist: + if not ComposedMessage.objects.filter(attachment_files__uuid=orphan_att.uuid).exists(): + if not StoredMessage.objects.filter(attachment_files__uuid=orphan_att.uuid).exists(): print('Deleting %s' % orphan_att.uuid) if orphan_att.file: if os.path.isfile(orphan_att.file.path):