How to Delete All Documents in a Collection Without Deleting the Collection

Introduction

Dropping a collection in MongoDB is very common but sometimes we found ourselves wanting to delete all documents in a collection without deleting the collection itself. This way we wouldn’t have to recreate the collection. If you find yourself in this same predicament please read on as we will go through a few options of doing this step-by-step.

Prerequisites

  • You should have MongoDB installed and running.
  • It’s recommended that you create a database and collection to experiment with as you follow along.
  • Some command line experience is recommended.

Goal

We like to set a clear goal for our tutorials. Our goal for this tutorial is to delete all records from our demoCollection which is located in our demoDatabase database.

1. Select database

First select the database you want to work with by utilizing the use <databaseName> command as follows

use demoDatabase

2. Find the Collection Name

Note: If already know collection name, you can skip this step. We need to know the exact name of the collection we want to work with. To do this execute a show collections command which lists all the collections in the current database.

> show collections
demoCollection
>

As you can see we only have one collection demoCollection in our database.

3. Remove all Documents with db.collection.remove({}) or db.collection.deleteMany({})

There are a few different options to delete all documents in a collection and for this tutorial we will go over the two easiest methods. 1. db.collection.remove({})


* This function optionally takes more parameters but if you are just deleting all documents as we are here, then we don't pass in any parameters like this `{}`.
  1. db.collection.deleteMany({})
    • The second method, deleteMany, allows us to delete multiple documents but again if we pass in no parameters {} it will delete all documents in the collection.

Before remove let’s see if there is any data in our collectin using the db.collection.find() command

> db.demoCollection.find()
{ "_id" : ObjectId("5c79ac2aad685885625abf46"), "userId" : 2, "name" : "Betty" }
{ "_id" : ObjectId("5c79ac2aad685885625abf47"), "userId" : 1, "name" : "Al" }
{ "_id" : ObjectId("5c79ac2aad685885625abf48"), "userId" : 3, "name" : "Cameron" }

Now let’s remove all documents from collection using the first option .remove()

Option 1:

> db.demoCollection.remove({})
WriteResult({ "nRemoved" : 3 })

You can confirm from the output message that 3 objects have been removed.

Now let’s remove all documents from collection using the second option .deleteMany()

Option 2:

> db.demoCollection.deleteMany({})
{ "acknowledged" : true, "deletedCount" : 3 }

Again, you can confirm from the output message that 3 objects have been removed.

Conclusion

We showed you two ways to delete all documents in a MongoDB collecion using the .remove() function and the .deleteMany() function. If you’d like to learn more about the options available in these functions, the MongoDB documentations is fairly straightforward and can answer many of your questions. We hope this tutorial was able to answer your question or provide you the snippet of code that you were looking for all along. Thank you for your time and if you have any feedback or questions do not hesitate to reach out to us.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.