how to delete a collection in mongodb database

Methods to Delete a Collection in MongoDB

In this tutorial we will learn 2 methods of deleting a collection in MongoDB. The term for deletion is to drop the collection which we can drop whenever we find it unnecessary or we don’t need it anymore. Here we will use 2 methods to drop it using mongo shell.

The db.collection.drop() Method

We will use db.collection.drop() function to drop a collection/view we want to delete that will also delete any indexes related to it.



We can replace the collection with the name of the collection such as Pets, Animals or whatever that name be





It drops the Pets collection from our database.

Write Concern

So if we decide to specify write concern so we will use this syntax

db.collection.drop( { writeConcern:  } )

Where is the write concern.

Here’s an example that specifies a write concern:

db.owners.drop( { writeConcern: { w: "majority" } } )

The method db.collection.drop() wraps around the drop command here is how that is in detail.

The drop Command Method

We will see that as soon as we add drop parameter it drops or deletes the collection.

See below Syntax for it:

{ drop: <collection_name>, writeConcern: <document>, comment:   <any> }


is the name of the collection. You need to understand that WriteConcern can be used if we want to for specifying a document to express the write concern of the drop command. It explains the level of acknowledgement requested from the MongoDB. While the comment is at our discretion user-provided comment to attach to this command. A comment can be any valid BSON type (string, integer, object, array, etc).

Here is the example:

db.runCommand( { drop: "pets" } )


{ "nIndexesWas" : 1, "ns" : "Animals.pets", "ok" : 1 }

When we use the above Syntax it will drop or remove the collection pets from Animals database.