
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
How to delete all the documents from a collection in MongoDB?
If you want to delete all documents from the collection, you can use deleteMany(). Let us first create a collection and insert some documents to it:
> db.deleteDocumentsDemo.insert({"Name":"Larry","Age":23}); WriteResult({ "nInserted" : 1 }) > db.deleteDocumentsDemo.insert({"Name":"Mike","Age":21}); WriteResult({ "nInserted" : 1 }) > db.deleteDocumentsDemo.insert({"Name":"Sam","Age":24}); WriteResult({ "nInserted" : 1 })
Now display all the documents from the collection. The query is as follows:
> db.deleteDocumentsDemo.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6ab0e064f3d70fcc914805"), "Name" : "Larry", "Age" : 23 } { "_id" : ObjectId("5c6ab0ef64f3d70fcc914806"), "Name" : "Mike", "Age" : 21 } { "_id" : ObjectId("5c6ab0f864f3d70fcc914807"), "Name" : "Sam", "Age" : 24 }
The query is as follows:
> db.deleteDocumentsDemo.deleteMany({});
The following is the output:
{ "acknowledged" : true, "deletedCount" : 3 }
Look at the above sample output. Right now, we do not have any documents in the collection ‘deleteDocumentsDemo’ i.e. we have successfully deleted all the documents using the deleteMany() method.
- Related Articles
- How to delete documents from a collection in MongoDB?
- How to retrieve all the documents from a MongoDB collection using Java?
- Fetching all documents from MongoDB Collection in a beautified form
- How to retrieve documents from a collection in MongoDB?
- How to remove all documents from a collection except a single document in MongoDB?
- How to delete multiple documents from a collection using Java?
- Display only a single field from all the documents in a MongoDB collection
- How to add a new field to all the documents in a MongoDB collection
- How to sum the value of a key across all documents in a MongoDB collection?
- How to delete document from a collection in MongoDB using deleteOne() method?
- Get all embedded documents with “isMarried” status in a MongoDB collection
- How to count the number of documents in a MongoDB collection?
- Find all duplicate documents in a MongoDB collection by a key field?
- Delete a collection from MongoDB with special characters?
- Find a value in lowercase from a MongoDB collection with documents

Advertisements