diff --git a/integrations/pinecone/src/haystack_integrations/document_stores/pinecone/document_store.py b/integrations/pinecone/src/haystack_integrations/document_stores/pinecone/document_store.py index 2f7bfab649..7b7d81b093 100644 --- a/integrations/pinecone/src/haystack_integrations/document_stores/pinecone/document_store.py +++ b/integrations/pinecone/src/haystack_integrations/document_stores/pinecone/document_store.py @@ -11,6 +11,7 @@ from pinecone import Pinecone, PineconeAsyncio, PodSpec, ServerlessSpec from pinecone.db_data import _Index, _IndexAsyncio +from pinecone.exceptions import NotFoundException from .filters import _normalize_filters, _validate_filters @@ -352,6 +353,30 @@ async def delete_documents_async(self, document_ids: List[str]) -> None: assert self._async_index is not None, "Index is not initialized" await self._async_index.delete(ids=document_ids, namespace=self.namespace) + def delete_all_documents(self) -> None: + """ + Deletes all documents in the document store. + """ + self._initialize_index() + assert self._index is not None, "Index is not initialized" + try: + self._index.delete(delete_all=True, namespace=self.namespace) + except NotFoundException: + # Namespace doesn't exist (empty collection), which is fine - nothing to delete + logger.debug("Namespace '{namespace}' not found. Nothing to delete.", namespace=self.namespace or "default") + + async def delete_all_documents_async(self) -> None: + """ + Asynchronously deletes all documents in the document store. + """ + await self._initialize_async_index() + assert self._async_index is not None, "Index is not initialized" + try: + await self._async_index.delete(delete_all=True, namespace=self.namespace) + except NotFoundException: + # Namespace doesn't exist (empty collection), which is fine - nothing to delete + logger.debug("Namespace '{namespace}' not found. Nothing to delete.", namespace=self.namespace or "default") + def _embedding_retrieval( self, query_embedding: List[float], diff --git a/integrations/pinecone/tests/test_document_store.py b/integrations/pinecone/tests/test_document_store.py index 21ebc8136c..152ac436d4 100644 --- a/integrations/pinecone/tests/test_document_store.py +++ b/integrations/pinecone/tests/test_document_store.py @@ -272,6 +272,19 @@ def test_write_documents_duplicate_skip(self, document_store: PineconeDocumentSt @pytest.mark.skip(reason="Pinecone creates a namespace only when the first document is written") def test_delete_documents_empty_document_store(self, document_store: PineconeDocumentStore): ... + def test_delete_all_documents(self, document_store: PineconeDocumentStore): + docs = [Document(content="first doc"), Document(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: PineconeDocumentStore): + assert document_store.count_documents() == 0 + document_store.delete_all_documents() + assert document_store.count_documents() == 0 + def test_embedding_retrieval(self, document_store: PineconeDocumentStore): query_embedding = [0.1] * 768 most_similar_embedding = [0.8] * 768 diff --git a/integrations/pinecone/tests/test_document_store_async.py b/integrations/pinecone/tests/test_document_store_async.py index 83c1959f19..60f1abed54 100644 --- a/integrations/pinecone/tests/test_document_store_async.py +++ b/integrations/pinecone/tests/test_document_store_async.py @@ -66,6 +66,19 @@ async def test_delete_documents(self, document_store_async: PineconeDocumentStor await document_store_async.delete_documents_async([doc.id]) assert await document_store_async.count_documents_async() == 0 + async def test_delete_all_documents_async(self, document_store_async: PineconeDocumentStore): + docs = [Document(content="first doc"), Document(content="second doc")] + await document_store_async.write_documents_async(docs) + assert await document_store_async.count_documents_async() == 2 + + await document_store_async.delete_all_documents_async() + assert await document_store_async.count_documents_async() == 0 + + async def test_delete_all_documents_async_empty_collection(self, document_store_async: PineconeDocumentStore): + assert await document_store_async.count_documents_async() == 0 + await document_store_async.delete_all_documents_async() + assert await document_store_async.count_documents_async() == 0 + async def test_embedding_retrieval(self, document_store_async: PineconeDocumentStore): query_embedding = [0.1] * 768 most_similar_embedding = [0.8] * 768