delete_metabase_collection
Remove a Metabase collection by its ID to clean up BI assets and organize your data workspace.
Instructions
Delete a Metabase collection.
Args: collection_id (int): ID of the collection to delete.
Returns: Dict[str, Any]: Confirmation of the collection deletion.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection_id | Yes |
Implementation Reference
- src/metabase_mcp_server.py:309-321 (handler)Handler function for 'delete_metabase_collection' tool. Takes collection_id as integer parameter and makes a DELETE request to Metabase API endpoint /api/collection/{collection_id}. Registered with @mcp.tool() decorator. Returns Dict[str, Any] with deletion confirmation.
@mcp.tool() async def delete_metabase_collection(collection_id: int) -> Dict[str, Any]: """ Delete a Metabase collection. Args: collection_id (int): ID of the collection to delete. Returns: Dict[str, Any]: Confirmation of the collection deletion. """ logger.info(f"Deleting collection {collection_id}") return await make_metabase_request(RequestMethod.DELETE, f"/api/collection/{collection_id}") - src/metabase_mcp_server.py:162-247 (helper)Core helper function 'make_metabase_request' that handles HTTP requests to Metabase API. Used by delete_metabase_collection to execute DELETE requests. Manages session, authentication, error handling, and response parsing. Raises MetabaseConnectionError or MetabaseResponseError on failures.
async def make_metabase_request( method: RequestMethod, endpoint: str, data: Optional[Dict[str, Any] | bytes] = None, params: Optional[Dict[str, Any]] = None, json: Any = None, headers: Optional[Dict[str, str]] = None, ) -> Dict[str, Any]: """ Make a request to the Metabase API. Args: method: HTTP method to use (GET, POST, PUT, DELETE) endpoint: API endpoint path data: Request data (for form data) params: URL parameters json: JSON request body headers: Additional headers Returns: Dict[str, Any]: Response data Raises: MetabaseConnectionError: When the Metabase server is unreachable MetabaseResponseError: When Metabase returns a non-2xx status code RuntimeError: For other errors """ if not METABASE_URL or not METABASE_API_KEY: raise RuntimeError("METABASE_URL or METABASE_API_KEY environment variable is not set. Metabase API requests will fail.") if session is None: raise RuntimeError("HTTP session is not initialized. Ensure app_lifespan was called.") try: request_headers = headers or {} logger.debug(f"Making {method.name} request to {METABASE_URL}{endpoint}") # Log request payload for debugging (omit sensitive info) if json and logger.level <= logging.DEBUG: sanitized_json = {**json} if 'password' in sanitized_json: sanitized_json['password'] = '********' logger.debug(f"Request payload: {sanitized_json}") response = await session.request( method=method.name, url=endpoint, timeout=aiohttp.ClientTimeout(total=30), headers=request_headers, data=data, params=params, json=json, ) try: # Handle 500 errors with more detailed info if response.status >= 500: error_text = await response.text() logger.error(f"Server error {response.status}: {error_text[:200]}") raise MetabaseResponseError(response.status, f"Server Error: {error_text[:200]}", endpoint) response.raise_for_status() response_data = await response.json() # Ensure the response is a dictionary for FastMCP compatibility return ensure_dict_response(response_data) except aiohttp.ContentTypeError: # Handle empty responses or non-JSON responses content = await response.text() if not content: return {"data": {}} logger.warning(f"Received non-JSON response: {content}") return {"data": content} except aiohttp.ClientConnectionError as e: logger.error(f"Connection error: {str(e)}") raise MetabaseConnectionError("Metabase is unreachable. Is the Metabase server running?") from e except aiohttp.ClientResponseError as e: logger.error(f"Response error: {e.status}, {e.message}, {e.request_info.url}") raise MetabaseResponseError(e.status, e.message, str(e.request_info.url)) from e except Exception as e: logger.error(f"Request error: {str(e)}") raise RuntimeError(f"Request error: {str(e)}") from e - src/enums/request_enum.py:1-11 (schema)RequestMethod enum defining HTTP methods including DELETE, which is used by delete_metabase_collection handler to specify the HTTP DELETE method for the API request.
from enum import Enum, auto class RequestMethod(Enum): GET = auto() POST = auto() PUT = auto() DELETE = auto() def __str__(self): return self.name - src/errors/metabase_errors.py:1-15 (helper)Custom exception classes for Metabase API errors. MetabaseConnectionError raised when server is unreachable, MetabaseResponseError raised for non-2xx status codes. Used by make_metabase_request helper function for error handling in delete_metabase_collection tool.
class MetabaseRequestError(Exception): """Base exception for Metabase API request errors""" pass class MetabaseConnectionError(MetabaseRequestError): """Exception raised when Metabase server is unreachable""" pass class MetabaseResponseError(MetabaseRequestError): """Exception raised when Metabase returns a non-2xx status code""" def __init__(self, status, message, url): self.status = status self.message = message self.url = url super().__init__(f"Status {status}, message: '{message}', url: '{url}'")