Delete
Learn how to delete documents in MongoDB using deleteOne, deleteMany, and how to drop collections and databases.
Delete
The fourth and final operation in CRUD is Delete — removing documents from a collection. MongoDB gives you two methods for this:
deleteOne()— delete the first document that matches the filterdeleteMany()— delete all documents that match the filter
There is also drop() — which removes an entire collection at once.
deleteOne
deleteOne() finds the first document that matches the filter and removes it permanently.
Syntax
db.collection.deleteOne(filter)Example — Delete a student by name
db.students.deleteOne({ name: "Bilal Ahmed" })Result
{
acknowledged: true,
deletedCount: 1
}acknowledged: true— the operation was received and executeddeletedCount: 1— one document was deleted
Example — Delete by _id
Deleting by _id is the safest approach — it guarantees you delete exactly the right document:
const { ObjectId } = require('mongodb');
db.students.deleteOne({
_id: new ObjectId("64a1f2c3e4b0a1b2c3d4e5f6")
})deleteMany
deleteMany() removes all documents that match the filter.
Syntax
db.collection.deleteMany(filter)Example — Delete all unenrolled students
db.students.deleteMany({ enrolled: false })Result
{
acknowledged: true,
deletedCount: 2
}Example — Delete all students in a specific grade
db.students.deleteMany({ grade: "9th" })Delete all documents in a collection
Pass an empty filter {} to delete every document in the collection:
db.students.deleteMany({})deleteMany({}) deletes every single document in the collection. The collection itself still exists — it just becomes empty. This cannot be undone. Always double-check your filter before running deleteMany.
drop
drop() removes an entire collection — all its documents, indexes, and metadata — in one operation.
db.students.drop()Result
trueMongoDB returns true if the collection was dropped successfully.
drop() is faster than deleteMany({}) when you want to remove everything because it does not go through documents one by one — it just deletes the whole collection at once.
deleteMany({}) | drop() | |
|---|---|---|
| Removes documents | ✅ Yes | ✅ Yes |
| Removes indexes | ❌ No | ✅ Yes |
| Removes collection | ❌ No | ✅ Yes |
| Speed | Slower | Faster |
| Use when | Clearing data, keeping structure | Removing everything completely |
Drop a Database
To remove an entire database — all its collections and all their documents:
use school
db.dropDatabase()dropDatabase() is irreversible. Everything in the database is gone permanently. Never run this on a production database unless you are absolutely certain. Always take a backup first.
What Happens When Nothing Matches
If your filter does not match any document, MongoDB does not throw an error — it just returns deletedCount: 0:
db.students.deleteOne({ name: "Someone Who Does Not Exist" })Result:
{
acknowledged: true,
deletedCount: 0
}Always check deletedCount in your code if you need to confirm something was actually deleted.
Deleting in Node.js
const db = client.db('school');
const students = db.collection('students');
// deleteOne
const resultOne = await students.deleteOne({ name: "Bilal Ahmed" });
console.log(`Deleted: ${resultOne.deletedCount} document`);
// deleteMany
const resultMany = await students.deleteMany({ enrolled: false });
console.log(`Deleted: ${resultMany.deletedCount} documents`);
// Check if anything was deleted
if (resultOne.deletedCount === 0) {
console.log("No document found with that filter");
}Hard Delete vs Soft Delete
A hard delete is what we have been doing — the document is permanently removed from the database. Once deleted, it is gone.
A soft delete means you do not actually remove the document. Instead, you add a flag like deleted: true and filter it out in your queries. The data stays in the database but is treated as deleted.
Example — Soft delete a student
// Instead of deleting — mark as deleted
db.students.updateOne(
{ name: "Bilal Ahmed" },
{
$set: {
deleted: true,
deletedAt: new Date()
}
}
)Now when fetching students, always filter out deleted ones:
// Only get students that are not deleted
db.students.find({ deleted: { $ne: true } })When to use soft delete
Use soft delete when:
- You need an audit trail — who was deleted and when
- You might need to restore deleted records later
- Regulations require you to keep data for a certain period
- You want to show deleted items in an admin panel
Use hard delete when:
- The data is truly disposable — test data, temp records
- You are required to permanently erase data — for example privacy regulations like GDPR
- Storage cost matters and deleted data has no future value
Most real-world school systems use soft delete for students and teachers. A student leaving the school does not mean their academic records should be erased. Soft delete keeps the history while hiding the record from normal queries.
School System Delete Operations
// A student transferred out — soft delete
db.students.updateOne(
{ name: "Sara Ahmed" },
{
$set: {
deleted: true,
deletedAt: new Date(),
deletionReason: "Transferred to another school"
}
}
)
// Remove all students who never completed enrollment
db.students.deleteMany({
enrolled: false,
enrollmentDate: { $lt: new Date("2024-01-01") }
})
// A course was cancelled — delete it
db.courses.deleteOne({ code: "BIO-11" })
// Clear all test data from a temporary collection
db.testData.drop()CRUD Complete — Quick Recap
We have now covered all four CRUD operations. Here is a full summary:
| Operation | Method | What it does |
|---|---|---|
| Create | insertOne() | Insert one document |
| Create | insertMany() | Insert multiple documents |
| Read | findOne() | Fetch one document |
| Read | find() | Fetch multiple documents |
| Update | updateOne() | Update one document |
| Update | updateMany() | Update multiple documents |
| Update | replaceOne() | Replace one document entirely |
| Delete | deleteOne() | Delete one document |
| Delete | deleteMany() | Delete multiple documents |
| Delete | drop() | Delete entire collection |
Before deleting anything in a real application, ask yourself — will I ever need this data again? If there is any chance the answer is yes, use soft delete. Hard deletes are permanent and cannot be undone without a backup.