@@ -385,6 +385,44 @@ def delete_documents(self, document_ids: List[str]) -> None:
385385 if documents :
386386 self .client .delete_documents (documents )
387387
388+ def delete_all_documents (self , recreate_index : bool = False ) -> None : # noqa: FBT002, FBT001
389+ """
390+ Deletes all documents in the document store.
391+
392+ :param recreate_index: If True, the index will be deleted and recreated with the original schema.
393+ If False, all documents will be deleted while preserving the index.
394+ """
395+ try :
396+ if recreate_index :
397+ # Get current index definition
398+ if self ._index_client is None :
399+ msg = "Index client is not initialized"
400+ raise ValueError (msg )
401+ current_index = self ._index_client .get_index (self ._index_name )
402+
403+ # Delete and recreate index
404+ self ._index_client .delete_index (self ._index_name )
405+ self ._index_client .create_index (current_index )
406+ logger .info ("Index '{idx_name}' recreated with original schema." , idx_name = self ._index_name )
407+ else :
408+ # Delete all documents without recreating index
409+ if self .count_documents () == 0 :
410+ return
411+
412+ # Search for all documents (pagination handled by Azure SDK)
413+ all_docs = list (self .client .search (search_text = "*" , select = ["id" ], top = 100000 ))
414+
415+ if all_docs :
416+ self .client .delete_documents (all_docs )
417+ logger .info (
418+ "Deleted {n_docs} documents from index '{idx_name}'." ,
419+ n_docs = len (all_docs ),
420+ idx_name = self ._index_name ,
421+ )
422+ except Exception as e :
423+ msg = f"Failed to delete all documents from Azure AI Search: { e !s} "
424+ raise HttpResponseError (msg ) from e
425+
388426 def get_documents_by_id (self , document_ids : List [str ]) -> List [Document ]:
389427 return self ._convert_search_result_to_documents (self ._get_raw_documents_by_id (document_ids ))
390428
0 commit comments