From 7d863a553afdbb026f2ee26ef71c01f9a669375a Mon Sep 17 00:00:00 2001 From: stumpylog <797416+stumpylog@users.noreply.github.com> Date: Fri, 17 Jul 2026 11:22:21 -0700 Subject: [PATCH] Stream documents in document_archiver via .only() + .iterator() document_archiver built its document_ids list by materializing every full Document instance (all fields) just to read has_archive_version. Restrict the queryset to the two fields actually used and stream it with .iterator() to avoid loading the whole table into memory at once. Co-Authored-By: Claude Sonnet 5 --- src/documents/management/commands/document_archiver.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/documents/management/commands/document_archiver.py b/src/documents/management/commands/document_archiver.py index bd3484321..90238b1d6 100644 --- a/src/documents/management/commands/document_archiver.py +++ b/src/documents/management/commands/document_archiver.py @@ -54,8 +54,12 @@ class Command(PaperlessCommand): else: documents = Document.objects.all() + documents = documents.only("id", "archive_filename") + document_ids = [ - doc.id for doc in documents if overwrite or not doc.has_archive_version + doc.id + for doc in documents.iterator(chunk_size=2000) + if overwrite or not doc.has_archive_version ] try: