delete_document
Remove specific documents from an Elasticsearch index by specifying the index and document ID, ensuring precise data management in knowledge systems.
Instructions
Delete a document from Elasticsearch index by document ID
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| doc_id | Yes | Document ID to delete from the index | |
| index | Yes | Name of the Elasticsearch index containing the document |
Implementation Reference
- Core handler implementation for the 'delete_document' tool. Deletes the specified document from Elasticsearch with detailed error handling for connection issues, missing indices, and non-existent documents.@app.tool( description="Delete a document from Elasticsearch index by document ID", tags={"elasticsearch", "delete", "document"} ) async def delete_document( index: Annotated[str, Field(description="Name of the Elasticsearch index containing the document")], doc_id: Annotated[str, Field(description="Document ID to delete from the index")] ) -> str: """Delete a document from Elasticsearch index.""" try: es = get_es_client() result = es.delete(index=index, id=doc_id) return f"β Document deleted successfully:\n\n{json.dumps(result, indent=2, ensure_ascii=False)}" except Exception as e: # Provide detailed error messages for different types of Elasticsearch errors error_message = "β Failed to delete document:\n\n" error_str = str(e).lower() if "connection" in error_str or "refused" in error_str: error_message += "π **Connection Error**: Cannot connect to Elasticsearch server\n" error_message += f"π Check if Elasticsearch is running at the configured address\n" error_message += f"π‘ Try: Use 'setup_elasticsearch' tool to start Elasticsearch\n\n" elif ( "not_found" in error_str or "not found" in error_str or "does not exist" in error_str) or "index_not_found_exception" in error_str or "no such index" in error_str: # Check if it's specifically an index not found error if ("index" in error_str and ( "not found" in error_str or "not_found" in error_str or "does not exist" in error_str)) or "index_not_found_exception" in error_str or "no such index" in error_str: error_message += f"π **Index Not Found**: Index '{index}' does not exist\n" error_message += f"π The target index has not been created yet\n" error_message += f"π‘ Try: Use 'list_indices' to see available indices\n\n" else: error_message += f"π **Document Not Found**: Document ID '{doc_id}' does not exist\n" error_message += f"π Cannot delete a document that doesn't exist\n" error_message += f"π‘ Try: Check document ID or use 'search' to find documents\n\n" else: error_message += f"β οΈ **Unknown Error**: {str(e)}\n\n" error_message += f"π **Technical Details**: {str(e)}" return error_message
- src/elasticsearch/elasticsearch_server.py:24-51 (registration)Registration of the delete_document tool via mounting of the elasticsearch_document sub-server into the unified Elasticsearch server.# Import sub-server applications for mounting from .sub_servers.elasticsearch_snapshots import app as snapshots_app from .sub_servers.elasticsearch_index_metadata import app as index_metadata_app from .sub_servers.elasticsearch_document import app as document_app from .sub_servers.elasticsearch_index import app as index_app from .sub_servers.elasticsearch_search import app as search_app from .sub_servers.elasticsearch_batch import app as batch_app # Create unified FastMCP application app = FastMCP( name="AgentKnowledgeMCP-Elasticsearch", version="2.0.0", instructions="Unified Elasticsearch tools for comprehensive knowledge management via modular server mounting" ) # ================================ # SERVER MOUNTING - MODULAR ARCHITECTURE # ================================ print("ποΈ Mounting Elasticsearch sub-servers...") # Mount all sub-servers into unified interface app.mount(snapshots_app) # 3 tools: snapshot management app.mount(index_metadata_app) # 3 tools: metadata governance app.mount(document_app) # 3 tools: document operations app.mount(index_app) # 3 tools: index management app.mount(search_app) # 2 tools: search & validation app.mount(batch_app) # 2 tools: batch operations
- src/main_server.py:74-86 (registration)Final top-level registration by mounting the Elasticsearch server (which includes delete_document) into the main FastMCP server.# Mount Elasticsearch server with 'es' prefix # This provides: es_search, es_index_document, es_create_index, etc. app.mount(elasticsearch_server_app) # Mount Administrative operations server with 'admin' prefix # This provides: admin_get_config, admin_update_config, admin_server_status, etc. app.mount(admin_server_app) # Mount Prompt server for AgentKnowledgeMCP guidance # This provides: usage_guide, help_request (prompts for LLM assistance) app.mount(prompt_server_app) print("π Server composition completed successfully!")