Open In App

Mongoose deleteMany() Function

Last Updated : 27 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The deleteMany() function is employed to remove all documents meeting specified conditions from a collection. Unlike the remove() function, deleteMany() deletes all matching documents without considering the single option. This method is essential for Node.js developers working with Mongoose, as it allows us to perform bulk deletions based on specific conditions, simplifying data management in MongoDB.

How to Use Mongoose deleteMany() Function?

Mongoose’s deleteMany() function is a powerful method to remove multiple documents from a MongoDB collection based on specified conditions. This method is often used in Node.js applications where we need to delete several records that match a given filter. Unlike the remove() function, deleteMany() allows us to delete all matching documents at once without considering the single document option.

Key Features of deleteMany()

  • Deletes Multiple Documents: The primary feature of deleteMany() is its ability to remove multiple documents based on the filter you provide.
  • Does Not Return Documents: Unlike other methods like find(), deleteMany() does not return the deleted documents, but rather, it returns a result object with information about the operation.
  • Promised-based: It returns a promise that resolves once the operation is completed, making it compatible with modern async/await syntax in Node.js.

Syntax:

Model.deleteMany()

Parameters: 

  • filter: A query object that specifies the conditions for deleting documents.
  • options (optional): An object that may contain additional options for controlling the delete operation, such as writeConcern.
  • callback (optional): A function that is executed once the operation completes. It will contain any error or result data.

Returns: 

The Model.deleteMany() function returns a promise. The result contains an array of objects having details of documents inserted in the database.

Steps to Installation of Mongoose Module:

Step 1: You can visit the link Install Mongoose module. You can install this package by using this command.

npm install mongoose

Step 2: After installing the mongoose module, you can check your mongoose version in the command prompt using the command.

npm version mongoose

Project Structure:

NodeProj

The updated dependencies in package.json file will look like:

"dependencies": {
"mongoose": "^7.6.5",
}

Example of Using deleteMany() in Mongoose

Let’s look at a practical example that demonstrates how to use the deleteMany() function to delete multiple documents from a MongoDB collection.

const mongoose = require('mongoose');

// Database connection
mongoose.connect(
'mongodb://127.0.0.1:27017/geeksforgeeks',
{
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
});

// User model
const User = mongoose.model(
'User',
{
name: { type: String },
age: { type: Number }
});

// Function call
// Deleting all users whose age >= 15
User.deleteMany(
{
age: { $gte: 15 }
}).then(
function () {
// Success
console.log("Data deleted");
}).catch(
function (error) {
// Failure
console.log(error);
});

Steps to run the program:

node index.js

Output: Below is the sample data in the database before the deleteMany() function is executed:Database after delete commandConsole Output:

Output of above command

After running above command, you can see the data is deleted from the database. Database after delete command

Conclusion

The deleteMany() function in Mongoose is an efficient way to delete multiple documents from a collection in MongoDB. It is especially useful for bulk delete operations, where we need to remove documents based on specific conditions. With its simple syntax, promise-based execution, and ease of use, deleteMany() is an essential tool in Mongoose for managing our data. By using this method, we can easily manage data removal in your applications, making our MongoDB operations more efficient and powerful.


Next Article

Similar Reads