Delete_Folder
Remove empty folders from SharePoint by specifying the folder path using the MCP server integration, streamlining document and folder management.
Instructions
Delete an empty folder from SharePoint
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| folder_path | Yes |
Implementation Reference
- src/mcp_sharepoint/tools.py:148-148 (registration)Registers the Delete_Folder tool using the @mcp.tool decorator.
@mcp.tool(name="Delete_Folder", description="Delete an empty folder from SharePoint") - src/mcp_sharepoint/tools.py:150-174 (handler)The main handler function for the Delete_Folder tool. It checks if the folder exists and is empty before deleting it using SharePoint API.
async def delete_folder(folder_path: str): """Delete an empty folder from SharePoint""" logger.info(f"Deleting folder: {folder_path}") # Get folder and check if it exists and is empty full_path = _get_path(folder_path) folder = sp_context.web.get_folder_by_server_relative_url(full_path) sp_context.load(folder) sp_context.load(folder.files) sp_context.load(folder.folders) sp_context.execute_query() if not hasattr(folder, 'exists') or not folder.exists: return {"success": False, "message": f"Folder '{folder_path}' does not exist"} if len(folder.files) > 0: return {"success": False, "message": f"Folder contains {len(folder.files)} files"} if len(folder.folders) > 0: return {"success": False, "message": f"Folder contains {len(folder.folders)} subfolders"} # Delete the empty folder folder.delete_object() sp_context.execute_query() return {"success": True, "message": f"Folder '{folder_path}' deleted successfully"} - src/mcp_sharepoint/tools.py:13-22 (helper)Helper decorator applied to the handler for error handling in SharePoint operations.
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 to construct the full SharePoint path for the folder.
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