Table of contents
1.
Introduction
2.
Prerequisites
3.
Delete the document using Mongoose methods
3.1.
1. Remove Documents
3.2.
2. Delete One Document
3.3.
3. Delete Many Documents 
3.4.
4. Delete Document by ID
4.
Delete the document using Mongoose Delete Plugin
4.1.
Features of mongoose-delete:-
4.2.
Installing mongoose-delete:-
4.3.
Usage of the mongoose-delete plugin:-
5.
Frequently Asked Questions
6.
Key Takeaways
Last Updated: Aug 13, 2025

Delete the documents using Mongoose

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

If you’re using the Mongoose library to interact with MongoDB from NodeJS, then this article will provide you with ample options to delete the document using Mongoose from the MongoDB database. At the end of this article, you will learn how to delete a single document by id, all documents from a collection, or documents matching a specific condition; and you can select the best way to delete the document using Mongoose as per your requirements.

Prerequisites

  • It would help if you had NodeJs installed on your system. Steps to setup NodeJs is provided for Windows and Linux
  • It would be best to install Mongoose and connect with the Mongodb database.
  • Essential Command-line experience is a recommendation.

Delete the document using Mongoose methods

For the whole article, we will use a Model called blogScheme with properties titledate, and body to modify and perform delete operations.

const blogSchema = new Schema({

    // String is shorthand for {type: String}

    title:  String, 
    date: { type: Date, default: Date.now },
    body:   String,
});
You can also try this code with Online Javascript Compiler
Run Code

1. Remove Documents

  • Model.remove():- As the name suggests, the remove() function in Mongoose is used to remove the documents from the Database based upon the provided conditions.

 

Note:- remove() function is now deprecated for mongoose v5.5.3 and above. We use deleteOne(), deleteMany(), etc., that we will see in the article next. 

The below code will remove all documents having date less than "01/09/2021", from database, and then prints "Deleted Blogs" in the console.

blogSchema.remove(({ date:{"$lt":new Date(2021,9, 1) } }), function (err) {
   if (err){
       console.log(err)
   }
   else{
       console.log("Deleted Blogs");
   }
});
You can also try this code with Online Javascript Compiler
Run Code

2. Delete One Document

  • Model.deleteOne():- The deleteOne() function can delete the first documents matching the conditions provided from the collection. This function can delete at most one document. 

 

We use the deleteOne() function if we need to delete only the first document matching the condition. 

The below code deletes the first document with the title "Coding Ninja Tutorial."

blogSchema.deleteOne({ title: ‘Coding Ninja Tutorial’ }).then(function(){
   console.log("Blog deleted"); // Success
}).catch(function(error){
   console.log(error); // Failure
});
You can also try this code with Online Javascript Compiler
Run Code
  • Model.findOneAndDelete():- This function is very similar to the deleteOne() function but provides few more options. This function finds the matching document from the collection, deletes it, and passes the found document to the callback, if any.

 

We use the findOneAndDelete() function if we need to delete the first document matching the condition and return the deleted document in the callback.

The below code deletes the first document having date less than "01/09/2021" and then prints the deleted blog in the console.

blogSchema.findOneAndDelete(({ date:{"$lt":new Date(2021,9, 1) } }), function (err, docs) {
   if (err){
       console.log(err)
   }
   else{
       console.log( docs);
   }
});
You can also try this code with Online Javascript Compiler
Run Code

3. Delete Many Documents 

  • Model.deleteMany():- We can use this function to delete multiple documents based on the conditions from the collection. The deleteMany() function deletes all the matching records depending on the conditions.

The below code deletes all documents having date less than "01/09/2021" and then prints "All the blogs deleted" in the console.

blogSchema.deleteMany({ date:{"$lt":new Date(2021,9, 1)} }).then(function(){
   console.log("All the blogs deleted"); // Success
}).catch(function(error){
   console.log(error); // Failure
});
You can also try this code with Online Javascript Compiler
Run Code

4. Delete Document by ID

  • Model.findByIdAndDelete() :- The findByIdAndDelete() function can be used to find a matching document by the id, deletes it and then passes the found document (if any) to the callback function.

We choose this function only when we have the document's id, and we need to delete the same record from the database.

Note: Under the hood findByIdAndDelete() function uses findOneAndDelete() that we discussed above providing id as condition.

The below code deletes the document having id '115eba4er5abde77b2efb20e' and then prints  the deleted blog in the console.

// Find a document whose id=115eba4er5abde77b2efb20e and remove it
const id = '115eba4er5abde77b2efb20e';

blogSchema.findByIdAndDelete(id, function (err, docs) {
   if (!err){
       console.log( docs);
   }
   else{
       console.log(err);
   }
});
You can also try this code with Online Javascript Compiler
Run Code

Delete the document using Mongoose Delete Plugin

We have seen the default methods available to delete the document using Mongoose. Now we will discover a simple and lightweight plugin, mongoose-delete, that enables soft deletion of documents in MongoDB. We get some great functionalities to delete the document using Mongoose Delete.

Features of mongoose-delete:-

With mongoose-delete, we can soft-delete documents instead of removing the document. Soft deletion of data means that we will add an extra property of deleted = true in the data, and by using this data, we will come to know that this data is deleted.

  • delete() method is added to the document. This method adds a few properties to the documents like deleted, deletedBy, deletedAt, etc.,
  • deletedById() static method is added.
  • restore() method is added to restore the deleted documents.
  • Bulk delete and restore.

Installing mongoose-delete:-

We can install the mongoose-delete plugin using npm.

npm install mongoose-delete
You can also try this code with Online Javascript Compiler
Run Code

Usage of the mongoose-delete plugin:-

The plugin provides us with many predefined functions and properties, which we will use in the below example and learn about their implementation.

First, we need to import the plugin and integrate it with our blogSchema.

var mongoose_delete = require('mongoose-delete');

const blogSchema = new Schema({
   title:  String, // String is shorthand for {type: String}
   date: { type: Date, default: Date.now },
   body:   String,
});

blogSchema.plugin(mongoose_delete);
var Blog = mongoose.model('Blog', blogSchema);
var newBlog = new Blog({title: ‘coding ninja blog’,
     body: ‘This is the body of blog’
})
You can also try this code with Online Javascript Compiler
Run Code

In the above code, we created a new model Blog and created a newBlog object using the blogSchema. Now, it's time to save this data in the database and then test the functions delete(), deleteByID(), and restore().

newBlog.save(function(){
//The json file saved in mongoDB is {deleted: false, title:‘coding ninja blog’, date: 05/12/2021, body: ‘This is the //body of blog’ }

	newBlog.delete( function(){
	//{deleted: true, title:‘coding ninja blog’, date: 05/12/2021, body: ‘This is the body of blog’ }

		newBlog.restore(function () {
    		// { deleted: false, title: ‘coding ninja    blog’, date: 05/12/2021, body: ‘This is the body of blog’ }
		});
    });
});

const id = '115eba4er5abde77b2efb20e';

newBlog.deleteById(id, function (err, docs) {
   // deletes the document with the id else returns the error.
});
You can also try this code with Online Javascript Compiler
Run Code

In the above example, the mongoose-delete plugin adds an extra property to the document deleted, which has a boolean SchemaType with a default value of false, which means that the document is not deleted. When we delete any document, the value of deleted property changes to true. Similarly, can also add other properties in the document like deletedAt ( key to store the time of deletion ), deletedBy ( key to store the user who deleted the document ).

And the restore() function sets the value of deleted property to false again to restore it.

Frequently Asked Questions

1. What is Mongoose?

Ans. Mongoose is an ODM (Object database Modeling) library that allows us to define objects with a schema that is mapped to a MongoDB document.

2. What is MongoDB?

Ans.  MongoDB is a document-oriented NoSQL database mainly used for large-scale and medium-scale data storage.

3. What is soft-deletion of documents?

Ans. Soft-deletion of documents in databases is an operation in which a flag is used to mark documents as deleted without erasing the data from the database.

Key Takeaways

In this article, we learned how to delete the document using Mongoose. We explored various methods and functions to delete the document using Mongoose; We also learned about the mongoose-delete plugin.

  • We can delete documents using mongoose with the help of different methods like Model.deleteOne(), Model.findOneAndDelete(), Model.deleteMany(), and Model.findByIdAndDelete().
  • We learned how to enable soft deletion of the document with mongoose-delete plugins, which has a lot of advantages if we need to restore the documents in the future or keep the metadata for the deleting operation.

 

If you want to learn advanced web development, Coding Ninjas has one of the best courses available, which you can find here.

Live masterclass