Paramclasses Academy - Blog

Tutorials Details

←
Post 14 of 35
→

MongoDB deleteOne() and deleteMany(): Remove Documents Safely

MongoDB deleteOne() and deleteMany(): Remove Documents Safely

November 5, 2026

 

MongoDB deleteOne() and deleteMany(): Remove Documents Safely

Welcome to Param Classes!

In MongoDB, managing data is not only about inserting and updating documents. Sometimes, we also need to remove unnecessary, outdated, or incorrect records from our database.

MongoDB provides two important methods for deleting documents:

  • deleteOne() – Deletes one matching document.

  • deleteMany() – Deletes all documents that match a specified condition.

In this tutorial, you will learn how to use both methods with practical MongoDB shell examples. You will also understand how to verify deleted records and avoid accidentally deleting important data.

✦ 1. What Is Document Deletion in MongoDB?

Document deletion means permanently removing one or more documents from a MongoDB collection.

For example, imagine a student management system containing student records. If a student record is no longer required, we can remove it using MongoDB delete methods.

MongoDB provides different deletion methods depending on how many matching documents you want to remove.

Main deletion methods:

  1. deleteOne()

  2. deleteMany()

✦ 2. Create a Sample Database and Collection

Before learning deletion methods, let's create a sample database.

Open MongoDB Shell (mongosh) and execute:

use college

Create a collection and insert sample student records:

db.students.insertMany([
    {
        name: "Rahul",
        age: 22,
        course: "MongoDB",
        status: "Active"
    },
    {
        name: "Amit",
        age: 24,
        course: "Java",
        status: "Active"
    },
    {
        name: "Priya",
        age: 21,
        course: "Python",
        status: "Inactive"
    },
    {
        name: "Neha",
        age: 26,
        course: "MongoDB",
        status: "Inactive"
    },
    {
        name: "Rohit",
        age: 23,
        course: "PHP",
        status: "Active"
    }
])

Display all documents:

db.students.find()

Now we have five student records in the collection.

✦ 3. What Is deleteOne() in MongoDB?

The deleteOne() method is used to delete only one document that matches a specified condition.

Even if multiple documents match the filter, deleteOne() deletes only one matching document.

Syntax:

db.collection.deleteOne(
    { condition }
)

Example:

Suppose we want to delete a student whose name is Rahul.

db.students.deleteOne({
    name: "Rahul"
})

This command deletes one document matching the name Rahul.

Expected result:

{
    acknowledged: true,
    deletedCount: 1
}

Here:

  • acknowledged: true means MongoDB acknowledged the operation.

  • deletedCount: 1 means one document was deleted.

To verify:

db.students.find()

The Rahul document will no longer appear in the results.

Important: If multiple documents have the same name, deleteOne() deletes only one matching document. If you need to target a particular record, use its unique _id.

✦ 4. Delete a Document Using _id

The safest way to delete a specific document is to use its unique _id value.

First, find the document:

db.students.findOne({
    name: "Amit"
})

You will see its _id value in the result.

Now use that exact _id:

db.students.deleteOne({
    _id: ObjectId("YOUR_OBJECT_ID")
})

Replace YOUR_OBJECT_ID with the actual ObjectId returned by MongoDB.

This ensures that the deletion targets the specific document.

✦ 5. What Is deleteMany() in MongoDB?

The deleteMany() method is used to delete all documents that match a specified condition.

For example, if we want to remove all inactive students, we can use deleteMany().

Syntax:

db.collection.deleteMany(
    { condition }
)

Example:

db.students.deleteMany({
    status: "Inactive"
})

This command deletes every document whose status is Inactive.

Expected result:

{
    acknowledged: true,
    deletedCount: 2
}

The exact deletedCount depends on the documents matching the filter.

To verify:

db.students.find()

Only the remaining documents will be displayed.

✦ 6. Delete Documents Using Comparison Operators

MongoDB also allows us to use comparison operators inside deletion filters.

For example, suppose we want to delete students whose age is greater than 24.

db.students.deleteMany({
    age: { $gt: 24 }
})

Here:

  • $gt means greater than.

  • 24 is the comparison value.

  • deleteMany() removes all documents matching the condition.

Other useful comparison operators include:

Operator Meaning
$eq Equal to
$ne Not equal to
$gt Greater than
$gte Greater than or equal to
$lt Less than
$lte Less than or equal to

Example:

db.students.deleteMany({
    age: { $lt: 22 }
})

This deletes all student documents where age is less than 22.

✦ 7. Delete Documents Using Logical Operators

MongoDB supports logical operators such as $and, $or, and $nor in deletion filters.

Example using $and:

db.students.deleteMany({
    $and: [
        { age: { $gt: 22 } },
        { status: "Inactive" }
    ]
})

This deletes documents where both conditions are true.

Example using $or:

db.students.deleteMany({
    $or: [
        { course: "PHP" },
        { course: "Java" }
    ]
})

This deletes documents where the course is either PHP or Java.

✦ 8. How to Check Deleted Documents

After deleting records, it is important to verify the result.

Find all remaining documents:

db.students.find()

Count remaining documents:

db.students.countDocuments()

Count documents matching a particular condition:

db.students.countDocuments({
    status: "Active"
})

These commands help confirm whether the expected documents were removed.

✦ 9. Difference Between deleteOne() and deleteMany()

Both methods delete documents, but they work differently.

Feature deleteOne() deleteMany()
Purpose Delete one matching document Delete all matching documents
Number of records Maximum one All matching records
Filter Required for targeted deletion Required for targeted deletion
Result deletedCount is 0 or 1 deletedCount can be 0 or more
Best use Removing one specific record Removing multiple matching records

Example:

db.students.deleteOne({
    course: "MongoDB"
})

Deletes only one matching document.

db.students.deleteMany({
    course: "MongoDB"
})

Deletes all documents matching the course condition.

✦ 10. Important Safety Warning: Avoid Empty Filters

One of the most important things to understand is what happens when you use an empty filter.

Dangerous example:

db.students.deleteMany({})

This deletes every document from the students collection.

Similarly:

db.students.deleteOne({})

This deletes one document from the collection, if one exists.

Always check your filter before running a deletion command, especially in a production database.

Safe practice:

First, preview the documents:

db.students.find({
    status: "Inactive"
})

If the displayed records are exactly the ones you want to remove, then execute:

db.students.deleteMany({
    status: "Inactive"
})

Never run a broad deletion query without checking which records it will affect.

✦ 11. Difference Between deleteMany() and drop()

MongoDB also provides the drop() method, but it is different from document deletion.

deleteMany() drop()
Deletes matching documents Removes the entire collection
Collection remains available Collection itself is removed
Can delete selected records Removes collection data and its indexes

Example:

db.students.deleteMany({})

This removes all documents but keeps the collection.

db.students.drop()

This removes the entire students collection.

Use drop() only when you really intend to remove the collection.

✦ 12. What Is findOneAndDelete()?

MongoDB also provides findOneAndDelete().

It finds one document matching a filter, deletes it, and returns the deleted document by default.

Syntax:

db.collection.findOneAndDelete({
    condition
})

Example:

db.students.findOneAndDelete({
    name: "Rohit"
})

This deletes one matching student document and returns the deleted document.

It is useful when your application needs the deleted record's information after deletion.

✦ 13. Can We Delete a Field Instead of a Document?

Yes, but deleting a field is different from deleting an entire document.

If you only want to remove a particular field, use the $unset update operator.

Example:

db.students.updateOne(
    { name: "Amit" },
    { $unset: { course: "" } }
)

This removes the course field from the matching document without deleting the entire student record.

✦ 14. Practical Exercise

Now practice the following MongoDB deletion commands.

Create a collection named employees:

db.employees.insertMany([
    { name: "Aman", age: 25, department: "IT" },
    { name: "Riya", age: 30, department: "HR" },
    { name: "Karan", age: 28, department: "IT" },
    { name: "Pooja", age: 22, department: "Sales" },
    { name: "Vikas", age: 35, department: "HR" }
])

Practice tasks:

  1. Delete one employee whose name is Aman.

  2. Delete all employees from the HR department.

  3. Delete employees whose age is less than 25.

  4. Display all remaining employee records.

  5. Count the remaining documents.

Try completing these tasks yourself using deleteOne(), deleteMany(), find(), and countDocuments().

✦ 15. Conclusion

In this tutorial, we learned how to delete documents in MongoDB using deleteOne() and deleteMany().

We also covered comparison operators, logical operators, document verification, findOneAndDelete(), and important deletion safety practices.

Remember:

  • Use deleteOne() when you want to delete only one matching document.

  • Use deleteMany() when you want to delete all matching documents.

  • Check your filter using find() before deleting records.

  • Be careful with empty filters because they can delete important data.

These methods are essential parts of MongoDB CRUD operations and are commonly used when developing real-world applications.

Keep practicing with Param Classes and improve your MongoDB database skills step by step.

✦ Official MongoDB Documentation

←
Post 14 of 35
→