From d6565232d7f0547701bc65e1181b7164d12ec2bc Mon Sep 17 00:00:00 2001 From: brendancicchi Date: Tue, 3 Sep 2024 09:54:07 -0400 Subject: [PATCH] #1047 Remove count_documents from delete_documents Removed the expensive check to see if the collection is non-empty by performing a full count. This is to fix issue #1047 --- .../document_stores/astra/document_store.py | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/integrations/astra/src/haystack_integrations/document_stores/astra/document_store.py b/integrations/astra/src/haystack_integrations/document_stores/astra/document_store.py index 1dea6e08b4..00817842a1 100644 --- a/integrations/astra/src/haystack_integrations/document_stores/astra/document_store.py +++ b/integrations/astra/src/haystack_integrations/document_stores/astra/document_store.py @@ -412,16 +412,13 @@ def delete_documents(self, document_ids: Optional[List[str]] = None, delete_all: :raises MissingDocumentError: if no document was deleted but document IDs were provided. """ deletion_counter = 0 - if self.index.count_documents() > 0: - if document_ids is not None: - for batch in _batches(document_ids, MAX_BATCH_SIZE): - deletion_counter += self.index.delete(ids=batch) - else: - deletion_counter = self.index.delete(delete_all=delete_all) - logger.info(f"{deletion_counter} documents deleted") - - if document_ids is not None and deletion_counter == 0: - msg = f"Document {document_ids} does not exist" - raise MissingDocumentError(msg) + if document_ids is not None: + for batch in _batches(document_ids, MAX_BATCH_SIZE): + deletion_counter += self.index.delete(ids=batch) else: - logger.info("No documents in document store") + deletion_counter = self.index.delete(delete_all=delete_all) + logger.info(f"{deletion_counter} documents deleted") + + if document_ids is not None and deletion_counter == 0: + msg = f"Document {document_ids} does not exist" + raise MissingDocumentError(msg)