Skip to main content
Glama

delete-post

Remove unwanted WordPress posts via REST API by specifying site URL, credentials, and post ID. Optionally bypass Trash for permanent deletion.

Instructions

Delete a WordPress post

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
forceNoWhether to bypass Trash and force deletion
passwordYesWordPress application password
postIdYesID of the post to delete
siteUrlYesWordPress site URL
usernameYesWordPress username

Implementation Reference

  • The main handler function for the 'delete-post' tool. It makes a DELETE request to the WordPress REST API endpoint `/wp-json/wp/v2/posts/{postId}` using the provided credentials and optional force parameter to either permanently delete or move the post to trash.
    async ({ siteUrl, username, password, postId, force }) => {
      try {
        await makeWPRequest<any>({
          siteUrl,
          endpoint: `posts/${postId}`,
          method: "DELETE",
          auth: { username, password },
          params: { force }
        });
        
        return {
          content: [
            {
              type: "text",
              text: `Successfully deleted post ${postId}${force ? " (forced deletion)" : " (moved to trash)"}.`,
            },
          ],
        };
      } catch (error) {
        return {
          content: [
            {
              type: "text",
              text: `Error deleting post: ${error instanceof Error ? error.message : String(error)}`,
            },
          ],
        };
      }
  • Zod schema defining the input parameters for the 'delete-post' tool, including site URL, credentials, post ID, and optional force flag.
    {
      siteUrl: z.string().url().describe("WordPress site URL"),
      username: z.string().describe("WordPress username"),
      password: z.string().describe("WordPress application password"),
      postId: z.number().describe("ID of the post to delete"),
      force: z.boolean().optional().default(false).describe("Whether to bypass Trash and force deletion"),
    },
  • src/index.ts:964-1003 (registration)
    The complete registration of the 'delete-post' tool using McpServer.tool(), including name, description, input schema, and handler function.
    server.tool(
      "delete-post",
      "Delete a WordPress post",
      {
        siteUrl: z.string().url().describe("WordPress site URL"),
        username: z.string().describe("WordPress username"),
        password: z.string().describe("WordPress application password"),
        postId: z.number().describe("ID of the post to delete"),
        force: z.boolean().optional().default(false).describe("Whether to bypass Trash and force deletion"),
      },
      async ({ siteUrl, username, password, postId, force }) => {
        try {
          await makeWPRequest<any>({
            siteUrl,
            endpoint: `posts/${postId}`,
            method: "DELETE",
            auth: { username, password },
            params: { force }
          });
          
          return {
            content: [
              {
                type: "text",
                text: `Successfully deleted post ${postId}${force ? " (forced deletion)" : " (moved to trash)"}.`,
              },
            ],
          };
        } catch (error) {
          return {
            content: [
              {
                type: "text",
                text: `Error deleting post: ${error instanceof Error ? error.message : String(error)}`,
              },
            ],
          };
        }
      }
    );
  • Shared helper function makeWPRequest used by the delete-post handler to perform authenticated HTTP requests to the WordPress REST API.
    async function makeWPRequest<T>({
      siteUrl, 
      endpoint,
      method = 'GET',
      auth,
      data = null,
      params = null
    }: {
      siteUrl: string;
      endpoint: string;
      method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
      auth: { username: string; password: string };
      data?: any;
      params?: any;
    }): Promise<T> {
      const authString = Buffer.from(`${auth.username}:${auth.password}`).toString('base64');
      
      try {
        const response = await axios({
          method,
          url: `${siteUrl}/wp-json/wp/v2/${endpoint}`,
          headers: {
            'Authorization': `Basic ${authString}`,
            'Content-Type': 'application/json',
          },
          data: data,
          params: params
        });
        
        return response.data as T;
      } catch (error) {
        if (axios.isAxiosError(error) && error.response) {
          throw new Error(`WordPress API error: ${error.response.data?.message || error.message}`);
        }
        throw error;
      }
    }

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/prathammanocha/wordpress-mcp-server'

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