delete_file
Remove files or directories from the system by specifying a path, with options for recursive deletion and error handling.
Instructions
Delete a file or directory
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Path to delete | |
| recursive | No | Delete directories recursively | |
| force | No | Ignore errors |
Implementation Reference
- src/tools/write.ts:434-470 (handler)The implementation of the delete file functionality.
async function deleteFileImpl(input: DeleteFileInput): Promise<ToolResult> { try { const absolutePath = path.resolve(input.path); // Check what type of path this is const stats = await fs.stat(absolutePath); if (stats.isDirectory()) { if (!input.recursive) { // Check if directory is empty const entries = await fs.readdir(absolutePath); if (entries.length > 0) { return { isError: true, content: [ { type: 'text', text: JSON.stringify({ code: 'DIRECTORY_NOT_EMPTY', message: `Directory is not empty and recursive is false: ${input.path}`, }), }, ], }; } } await fs.rm(absolutePath, { recursive: input.recursive, force: input.force }); } else { await fs.unlink(absolutePath); } return { content: [ { type: 'text', text: JSON.stringify({ - src/tools/write.ts:608-620 (registration)Registration of the 'delete_file' tool in the MCP server.
server.tool( 'delete_file', 'Delete a file or directory', { path: z.string().describe('Path to delete'), recursive: z.boolean().optional().describe('Delete directories recursively'), force: z.boolean().optional().describe('Ignore errors'), }, async (args) => { const input = DeleteFileInputSchema.parse(args); return await deleteFileImpl(input); } ); - src/types.ts:212-216 (schema)Schema definition for the delete_file tool input.
export const DeleteFileInputSchema = z.object({ path: z.string().describe('Path to delete'), recursive: z.boolean().default(false).describe('Delete directories recursively'), force: z.boolean().default(false).describe('Ignore errors'), });