Skip to main content
Glama
PWalaGov

Enhanced Directory Context MCP Server

by PWalaGov

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
NameRequiredDescriptionDefault
pathYesFile path relative to working directory
backupNoCreate backup before deleting (.deleted extension)

Implementation Reference

  • 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}`);
      }
    }
  • 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'],
      },
    },
  • Usage of handleDeleteFile as a helper in the batch_file_operations tool, with backup disabled.
    result = await this.handleDeleteFile({ ...params, backup: false });

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/PWalaGov/File-Control-MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server