Before deleting data, make sure you’re using the correct database
use myDatabase
Before deleting any records, it's a good practice to check the current data.
db.users.find().pretty()
To delete the first matching document, use deleteOne()
.
To delete all matching documents, use deleteMany()
.
db.users.deleteMany({ city: "New York" })
If you want to remove all documents but keep the collection structure, use:
db.users.deleteMany({})
✅ Deletes all records in the users
collection without removing the collection itself.
If you want to delete the entire collection (including its structure), use drop()
.
db.users.drop()
✅ Completely removes the users
collection from the database.
After deletion, check if the data is removed:
db.users.find().pretty()
You’ve learned how to delete data in MongoDB using:
✅ deleteOne()
– Deletes the first matching document.
✅ deleteMany()
– Deletes all matching documents.
✅ deleteMany({})
– Deletes all documents in a collection.
✅ drop()
– Removes the entire collection.
Use these methods carefully, as deletions cannot be undone! 🚀