delete-many
Remove multiple documents from a MongoDB collection based on specified filter criteria. Ideal for bulk deletion operations in MongoDB Atlas using MCP Server.
Instructions
Removes all documents that match the filter from a MongoDB collection
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection name | |
| database | Yes | Database name | |
| filter | No | The query filter, specifying the deletion criteria. Matches the syntax of the filter argument of db.collection.deleteMany() |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"collection": {
"description": "Collection name",
"type": "string"
},
"database": {
"description": "Database name",
"type": "string"
},
"filter": {
"additionalProperties": true,
"description": "The query filter, specifying the deletion criteria. Matches the syntax of the filter argument of db.collection.deleteMany()",
"properties": {},
"type": "object"
}
},
"required": [
"database",
"collection"
],
"type": "object"
}
Implementation Reference
- The execute method implements the core logic of the 'delete-many' tool: ensures connection, optionally checks index usage for the filter, executes deleteMany on the specified database and collection, and returns a result message with the count of deleted documents.protected async execute({ database, collection, filter, }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> { const provider = await this.ensureConnected(); // Check if delete operation uses an index if enabled if (this.config.indexCheck) { await checkIndexUsage(provider, database, collection, "deleteMany", async () => { return provider.runCommandWithCheck(database, { explain: { delete: collection, deletes: [ { q: filter || {}, limit: 0, // 0 means delete all matching documents }, ], }, verbosity: "queryPlanner", }); }); } const result = await provider.deleteMany(database, collection, filter); return { content: [ { text: `Deleted \`${result.deletedCount}\` document(s) from collection "${collection}"`, type: "text", }, ], }; }
- Input schema definition for the tool, extending DbOperationArgs with an optional filter using zEJSON for validation.protected argsShape = { ...DbOperationArgs, filter: zEJSON() .optional() .describe( "The query filter, specifying the deletion criteria. Matches the syntax of the filter argument of db.collection.deleteMany()" ), };
- src/tools/mongodb/tools.ts:9-9 (registration)Exports the DeleteManyTool class from its implementation file, making it available in the mongodb/tools module.export { DeleteManyTool } from "./delete/deleteMany.js";
- src/tools/index.ts:7-11 (registration)Registers the 'delete-many' tool by including the MongoDbTools module (which exports DeleteManyTool) in the global AllTools array used by the MCP server.export const AllTools: ToolClass[] = Object.values({ ...MongoDbTools, ...AtlasTools, ...AtlasLocalTools, });
- The full DeleteManyTool class definition, including name, description, schema, execute handler, and confirmation message generator.export class DeleteManyTool extends MongoDBToolBase { public name = "delete-many"; protected description = "Removes all documents that match the filter from a MongoDB collection"; protected argsShape = { ...DbOperationArgs, filter: zEJSON() .optional() .describe( "The query filter, specifying the deletion criteria. Matches the syntax of the filter argument of db.collection.deleteMany()" ), }; static operationType: OperationType = "delete"; protected async execute({ database, collection, filter, }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> { const provider = await this.ensureConnected(); // Check if delete operation uses an index if enabled if (this.config.indexCheck) { await checkIndexUsage(provider, database, collection, "deleteMany", async () => { return provider.runCommandWithCheck(database, { explain: { delete: collection, deletes: [ { q: filter || {}, limit: 0, // 0 means delete all matching documents }, ], }, verbosity: "queryPlanner", }); }); } const result = await provider.deleteMany(database, collection, filter); return { content: [ { text: `Deleted \`${result.deletedCount}\` document(s) from collection "${collection}"`, type: "text", }, ], }; } protected getConfirmationMessage({ database, collection, filter }: ToolArgs<typeof this.argsShape>): string { const filterDescription = filter && Object.keys(filter).length > 0 ? "```json\n" + `{ "filter": ${EJSON.stringify(filter)} }\n` + "```\n\n" : "- **All documents** (No filter)\n\n"; return ( `You are about to delete documents from the \`${collection}\` collection in the \`${database}\` database:\n\n` + filterDescription + "This operation will permanently remove all documents matching the filter.\n\n" + "**Do you confirm the execution of the action?**" ); } }