delete_items
Remove multiple files or directories at once from the filesystem by specifying their relative paths, streamlining cleanup and organization tasks.
Instructions
Delete multiple specified files or directories.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| paths | Yes | An array of relative paths (files or directories) to delete. |
Implementation Reference
- src/handlers/delete-items.ts:140-171 (handler)Main handler function for 'delete_items' tool: validates args, performs deletions on multiple paths concurrently with error handling using Promise.allSettled, processes and sorts results, returns JSON response.const handleDeleteItemsFunc = async (args: unknown): Promise<McpToolResponse> => { const { paths: pathsToDelete } = parseAndValidateArgs(args); const safeProcessSingleDeleteOperation = async (relativePath: string): Promise<DeleteResult> => { const pathOutput = relativePath.replaceAll('\\', '/'); try { // Call the core logic which might return a DeleteResult or throw return await processSingleDeleteOperation(relativePath); } catch (error) { // Catch errors thrown *before* the try block in processSingleDeleteOperation (like resolvePath) // or unexpected errors within it not returning a DeleteResult. return handleDeleteError(error, relativePath, pathOutput); } }; const deletePromises = pathsToDelete.map(safeProcessSingleDeleteOperation); const settledResults = await Promise.allSettled(deletePromises); const outputResults = processSettledResults(settledResults, pathsToDelete); // Sort results by original path order for predictability const originalIndexMap = new Map(pathsToDelete.map((p, i) => [p.replaceAll('\\', '/'), i])); outputResults.sort((a, b) => { const indexA = originalIndexMap.get(a.path) ?? Infinity; const indexB = originalIndexMap.get(b.path) ?? Infinity; return indexA - indexB; }); return { content: [{ type: 'text', text: JSON.stringify(outputResults, null, 2) }], }; };
- src/handlers/delete-items.ts:13-20 (schema)Zod schema defining the input for delete_items: object with 'paths' array of strings (non-empty).export const DeleteItemsArgsSchema = z .object({ paths: z .array(z.string()) .min(1, { message: 'Paths array cannot be empty' }) .describe('An array of relative paths (files or directories) to delete.'), }) .strict();
- src/handlers/delete-items.ts:174-179 (registration)Tool definition export for 'delete_items', specifying name, description, input schema, and handler reference.export const deleteItemsToolDefinition = { name: 'delete_items', description: 'Delete multiple specified files or directories.', inputSchema: DeleteItemsArgsSchema, handler: handleDeleteItemsFunc, };
- src/handlers/index.ts:48-53 (registration)Registration of deleteItemsToolDefinition (imported from './delete-items.js') in the central allToolDefinitions array used to expose tools.export const allToolDefinitions: HandlerToolDefinition[] = [ listFilesToolDefinition, statItemsToolDefinition, readContentToolDefinition, writeContentToolDefinition, deleteItemsToolDefinition,
- src/handlers/delete-items.ts:104-117 (helper)Core helper function that resolves path and performs single file/directory deletion using fs.rm (recursive), with root protection and error handling.async function processSingleDeleteOperation(relativePath: string): Promise<DeleteResult> { const pathOutput = relativePath.replaceAll('\\', '/'); try { const targetPath = await resolvePath(relativePath); if (targetPath === PROJECT_ROOT) { throw new McpError(ErrorCode.InvalidRequest, 'Deleting the project root is not allowed.'); } await fs.rm(targetPath, { recursive: true, force: false }); return { path: pathOutput, success: true }; } catch (error: unknown) { // This catch block will now correctly pass McpError or other errors to handleDeleteError return handleDeleteError(error, relativePath, pathOutput); } }