microcms_delete_media
Delete media files from microCMS via Management API. Supports images and files; fails if media is referenced by content.
Instructions
Delete media files from microCMS (Management API). Supports deletion of both images and files. Requires media deletion permissions. Note: Media referenced by content cannot be deleted.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| serviceId | No | Service ID (required in multi-service mode, optional in single-service mode) | |
| url | Yes | URL of the media to delete (e.g., "https://images.microcms-assets.io/assets/xxxxx/yyyyy/hoge.jpg" or "https://files.microcms-assets.io/assets/xxxxx/yyyyy/hoge.pdf"). Custom domain URLs are also supported. |
Implementation Reference
- src/tools/delete-media.ts:27-38 (handler)Handler function that executes the delete media logic. It extracts the 'url' param, validates it, and calls the client helper 'deleteMedia'.
export async function handleDeleteMedia( params: MediaToolParameters & { url: string }, serviceId?: string ) { const { url } = params; if (!url) { throw new Error('url is required'); } return await deleteMedia(url, serviceId); } - src/client.ts:474-496 (helper)Actual API call to microCMS Management API v2. Sends a DELETE request to /api/v2/media with the media URL as a query parameter.
export async function deleteMedia( mediaUrl: string, serviceId?: string ): Promise<{ id: string }> { const clients = getClientsForService(serviceId); const url = `https://${clients.serviceDomain}.microcms-management.io/api/v2/media?url=${encodeURIComponent(mediaUrl)}`; const response = await fetch(url, { method: 'DELETE', headers: { 'X-MICROCMS-API-KEY': clients.apiKey, }, }); if (!response.ok) { const errorText = await response.text(); throw new Error( `Failed to delete media: ${response.status} ${response.statusText} - ${errorText}` ); } return await response.json(); } - src/tools/delete-media.ts:5-25 (schema)Tool definition and input schema for microcms_delete_media. Defines the name, description, and input schema (requires 'url' string, optional 'serviceId').
export const deleteMediaTool: Tool = { name: 'microcms_delete_media', description: 'Delete media files from microCMS (Management API). Supports deletion of both images and files. Requires media deletion permissions. Note: Media referenced by content cannot be deleted.', inputSchema: { type: 'object', properties: { serviceId: { type: 'string', description: 'Service ID (required in multi-service mode, optional in single-service mode)', }, url: { type: 'string', description: 'URL of the media to delete (e.g., "https://images.microcms-assets.io/assets/xxxxx/yyyyy/hoge.jpg" or "https://files.microcms-assets.io/assets/xxxxx/yyyyy/hoge.pdf"). Custom domain URLs are also supported.', }, }, required: ['url'], }, }; - src/server.ts:127-127 (registration)Registration of the tool handler in the toolHandlers map, mapping 'microcms_delete_media' to handleDeleteMedia.
microcms_delete_media: handleDeleteMedia, - src/server.ts:34-34 (registration)Import of deleteMediaTool and handleDeleteMedia from delete-media module into the server.
import { deleteMediaTool, handleDeleteMedia } from './tools/delete-media.js';