Skip to main content
Glama

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
NameRequiredDescriptionDefault
pathYesPath to delete
recursiveNoDelete directories recursively
forceNoIgnore errors

Implementation Reference

  • 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({
  • 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);
      }
    );
  • 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'),
    });

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/mcp-tool-shop-org/mcp-file-forge'

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