Docs Menu
Docs Home
/ / /
Node.js Driver
/ /

Delete Multiple Documents

You can delete multiple 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 more options in the options object passed in the second parameter of the deleteMany() method. 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

You can use this example to connect to an instance of MongoDB and interact with a database that contains sample data. To learn more about connecting to your MongoDB instance and loading a sample dataset, see the Usage Examples guide.

1// Delete multiple documents
2
3import { MongoClient } from "mongodb";
4
5// Replace the uri string with your MongoDB deployment's connection string.
6const uri = "<connection string uri>";
7
8const client = new MongoClient(uri);
9
10async function run() {
11 try {
12 const database = client.db("sample_mflix");
13 const movies = database.collection("movies");
14
15 /* Delete all documents that match the specified regular
16 expression in the title field from the "movies" collection */
17 const query = { title: { $regex: "Santa" } };
18 const result = await movies.deleteMany(query);
19
20 // Print the number of deleted documents
21 console.log("Deleted " + result.deletedCount + " documents");
22 } finally {
23 // Close the connection after the operation completes
24 await client.close();
25 }
26}
27// Run the program and print any thrown exceptions
28run().catch(console.dir);
1// Delete multiple documents
2
3import { MongoClient } from "mongodb";
4
5// Replace the uri string with your MongoDB deployment's connection string
6const uri = "<connection string uri>";
7
8const client = new MongoClient(uri);
9
10async function run() {
11 try {
12 const database = client.db("sample_mflix");
13 const movies = database.collection("movies");
14
15 /* Delete all documents that match the specified regular
16 expression in the title field from the "movies" collection */
17 const result = await movies.deleteMany({ title: { $regex: "Santa" } });
18
19 // Print the number of deleted documents
20 console.log("Deleted " + result.deletedCount + " documents");
21 } finally {
22 // Close the connection after the operation completes
23 await client.close();
24 }
25}
26// Run the program and print any thrown exceptions
27run().catch(console.dir);

Running the preceding example for the first time, you see the following output:

Deleted 19 documents

If you run the example more than once, you see the following output because you deleted the matching documents in the first run:

Deleted 0 documents

Back

Delete a Document