delete_file
Remove files from your directory with optional backup creation to prevent data loss during file management operations.
Instructions
Delete a file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | File path relative to working directory | |
| backup | No | Create backup before deleting (.deleted extension) |
Implementation Reference
- server.js:638-666 (handler)The handler function that implements the core logic of the 'delete_file' tool: resolves the file path, checks existence, optionally creates a backup (.deleted), deletes the file using fs.unlink, and returns a success message.async handleDeleteFile(args) { const { path: filePath, backup = true } = args; const fullPath = path.resolve(this.workingDirectory, filePath); try { // Check if file exists await fs.access(fullPath); // Create backup if requested if (backup) { const backupPath = `${fullPath}.deleted`; await fs.copyFile(fullPath, backupPath); } // Delete file await fs.unlink(fullPath); return { content: [ { type: 'text', text: `File deleted: ${filePath}${backup ? '\nBackup created: ' + filePath + '.deleted' : ''}`, }, ], }; } catch (error) { throw new McpError(ErrorCode.InternalError, `Failed to delete file: ${error.message}`); } }
- server.js:279-293 (schema)The input schema defining parameters for the 'delete_file' tool: path (required string), backup (optional boolean, default true).inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'File path relative to working directory', }, backup: { type: 'boolean', description: 'Create backup before deleting (.deleted extension)', default: true, }, }, required: ['path'], },
- server.js:482-483 (registration)The dispatch case in the central CallToolRequestSchema handler that routes 'delete_file' tool calls to the handleDeleteFile method.case 'delete_file': return await this.handleDeleteFile(args);
- server.js:276-294 (registration)The tool descriptor registration in the ListToolsRequestSchema response, including name, description, and input schema.{ name: 'delete_file', description: 'Delete a file', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'File path relative to working directory', }, backup: { type: 'boolean', description: 'Create backup before deleting (.deleted extension)', default: true, }, }, required: ['path'], }, },
- server.js:755-755 (helper)Usage of handleDeleteFile as a helper in the batch_file_operations tool, with backup disabled.result = await this.handleDeleteFile({ ...params, backup: false });