diff --git a/integrations/mongodb_atlas/src/haystack_integrations/document_stores/mongodb_atlas/document_store.py b/integrations/mongodb_atlas/src/haystack_integrations/document_stores/mongodb_atlas/document_store.py index 50a44b6363..ddbdcb234c 100644 --- a/integrations/mongodb_atlas/src/haystack_integrations/document_stores/mongodb_atlas/document_store.py +++ b/integrations/mongodb_atlas/src/haystack_integrations/document_stores/mongodb_atlas/document_store.py @@ -423,6 +423,22 @@ async def delete_documents_async(self, document_ids: List[str]) -> None: return await self._collection_async.delete_many(filter={"id": {"$in": document_ids}}) + def delete_all_documents(self) -> None: + """ + Deletes all documents in the document store. + """ + self._ensure_connection_setup() + assert self._collection is not None + self._collection.delete_many({}) + + async def delete_all_documents_async(self) -> None: + """ + Asynchronously deletes all documents in the document store. + """ + await self._ensure_connection_setup_async() + assert self._collection_async is not None + await self._collection_async.delete_many({}) + def _embedding_retrieval( self, query_embedding: List[float], diff --git a/integrations/mongodb_atlas/tests/test_document_store.py b/integrations/mongodb_atlas/tests/test_document_store.py index dccdb3c99a..a32e02d791 100644 --- a/integrations/mongodb_atlas/tests/test_document_store.py +++ b/integrations/mongodb_atlas/tests/test_document_store.py @@ -333,3 +333,15 @@ def test_custom_content_field(self): finally: database[collection_name].drop() + + def test_delete_all_documents(self, document_store: MongoDBAtlasDocumentStore): + docs = [Document(id="1", content="first doc"), Document(id="2", content="second doc")] + document_store.write_documents(docs) + assert document_store.count_documents() == 2 + document_store.delete_all_documents() + assert document_store.count_documents() == 0 + + def test_delete_all_documents_empty_collection(self, document_store: MongoDBAtlasDocumentStore): + assert document_store.count_documents() == 0 + document_store.delete_all_documents() + assert document_store.count_documents() == 0 diff --git a/integrations/mongodb_atlas/tests/test_document_store_async.py b/integrations/mongodb_atlas/tests/test_document_store_async.py index 94608e3dff..6a7648dcbc 100644 --- a/integrations/mongodb_atlas/tests/test_document_store_async.py +++ b/integrations/mongodb_atlas/tests/test_document_store_async.py @@ -127,3 +127,15 @@ async def test_delete_documents_async(self, document_store: MongoDBAtlasDocument assert await document_store.count_documents_async() == 1 await document_store.delete_documents_async(document_ids=["1"]) assert await document_store.count_documents_async() == 0 + + async def test_delete_all_documents_async(self, document_store: MongoDBAtlasDocumentStore): + docs = [Document(id="1", content="first doc"), Document(id="2", content="second doc")] + await document_store.write_documents_async(docs) + assert await document_store.count_documents_async() == 2 + await document_store.delete_all_documents_async() + assert await document_store.count_documents_async() == 0 + + async def test_delete_all_documents_async_empty_collection(self, document_store: MongoDBAtlasDocumentStore): + assert await document_store.count_documents_async() == 0 + await document_store.delete_all_documents_async() + assert await document_store.count_documents_async() == 0