Docs Menu

Docs HomeDevelop ApplicationsMongoDB DriversNode.js

Delete Multiple Documents

Note

If you specify a callback method, deleteMany() returns nothing. If you do not specify one, this method returns a Promise that resolves to the result object when it completes. See our guide on Promises and Callbacks for more information, or the API documentation for information on the result object.

You can delete several documents in a collection at once using the collection.deleteMany() method. Pass a query document to the deleteMany() method to specify a subset of documents in the collection to delete. If you do not provide a query document (or if you provide an empty document), MongoDB matches all documents in the collection and deletes them. While you can use deleteMany() to delete all documents in a collection, consider using drop() instead for better performance and clearer code.

You can specify additional options in the options object passed in the second parameter of the deleteMany() method. You can also pass a callback method as the optional third parameter. For more detailed information, see the deleteMany() API documentation.

The following snippet deletes multiple documents from the movies collection. It uses a query document that configures the query to match and delete movies with the title "Santa Claus".

Note

This example connects to an instance of MongoDB and uses a sample data database. To learn more about connecting to your MongoDB instance and loading this database, see the Usage Examples guide.

const { MongoClient } = require("mongodb");
// Replace the uri string with your MongoDB deployment's connection string.
const uri =
"mongodb+srv://<user>:<password>@<cluster-url>?writeConcern=majority";
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
async function run() {
try {
await client.connect();
const database = client.db("sample_mflix");
const movies = database.collection("movies");
// Query for all movies with the title "Santa Claus"
const query = { title: "Santa Claus" };
const result = await movies.deleteMany(query);
console.log("Deleted " + result.deletedCount + " documents");
} finally {
await client.close();
}
}
run().catch(console.dir);
←  Delete a DocumentCount Documents →