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
| Name | Required | Description | Default |
|---|---|---|---|
| force | No | Whether to bypass Trash and force deletion | |
| password | Yes | WordPress application password | |
| postId | Yes | ID of the post to delete | |
| siteUrl | Yes | WordPress site URL | |
| username | Yes | WordPress username |
Implementation Reference
- src/index.ts:974-1001 (handler)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)}`, }, ], }; }
- src/index.ts:967-973 (schema)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)}`, }, ], }; } } );
- src/index.ts:158-194 (helper)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; } }