Delete_Document
Remove specific documents from SharePoint directories by specifying folder and file names, enabling precise document management and cleanup.
Instructions
Delete a document from a SharePoint directory
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_name | Yes | ||
| folder_name | Yes |
Implementation Reference
- src/mcp_sharepoint/tools.py:130-130 (registration)Registration of the 'Delete_Document' tool using the @mcp.tool decorator, specifying the tool name and description.@mcp.tool(name="Delete_Document", description="Delete a document from a SharePoint directory")
- src/mcp_sharepoint/tools.py:131-146 (handler)The handler function 'delete_document' that implements the core logic for deleting a SharePoint document. It checks if the file exists, deletes it using the SharePoint CSOM API, and returns success/error responses. Includes input schema via type hints (folder_name: str, file_name: str).@_handle_sp_operation async def delete_document(folder_name: str, file_name: str): """Delete a document from a directory""" logger.info(f"Deleting document {file_name} from folder {folder_name}") # Check if file exists and delete file = sp_context.web.get_file_by_server_relative_url(_get_path(folder_name, file_name)) sp_context.load(file, ["Exists"]) sp_context.execute_query() if not file.exists: return {"success": False, "message": f"File {file_name} does not exist in folder {folder_name}"} file.delete_object() sp_context.execute_query() return {"success": True, "message": f"File {file_name} deleted successfully"}
- src/mcp_sharepoint/tools.py:13-22 (helper)Helper decorator applied to the delete_document handler for standardized SharePoint error handling and logging.def _handle_sp_operation(func): """Decorator for SharePoint operations with error handling""" @wraps(func) async def wrapper(*args, **kwargs): try: return await func(*args, **kwargs) except Exception as e: logger.error(f"Error in {func.__name__}: {str(e)}") return {"success": False, "message": f"Operation failed: {str(e)}"} return wrapper
- src/mcp_sharepoint/tools.py:8-11 (helper)Helper function used by delete_document to construct the full SharePoint server-relative path for the target document.def _get_path(folder: str = "", file: Optional[str] = None) -> str: """Construct SharePoint path from components""" path = f"{SHP_DOC_LIBRARY}/{folder}".rstrip('/') return f"{path}/{file}" if file else path