148

I've tried

db.users.remove(*)

Although it returns an error so how do I go about clearing all records?

AllJs
  • 1,760
  • 4
  • 27
  • 48

8 Answers8

272

The argument to remove() is a filter document, so passing in an empty document means 'remove all':

db.user.remove({})

However, if you definitely want to remove everything you might be better off dropping the collection. Though that probably depends on whether you have user defined indexes on the collection i.e. whether the cost of preparing the collection after dropping it outweighs the longer duration of the remove() call vs the drop() call.

More details in the docs.

glytching
  • 44,936
  • 9
  • 114
  • 120
61

You can delete all the documents from a collection in MongoDB, you can use the following:

db.users.remove({})

Alternatively, you could use the following method as well:

db.users.deleteMany({})

Follow the following MongoDB documentation, for further details.

To remove all documents from a collection, pass an empty filter document {} to either the db.collection.deleteMany() or the db.collection.remove() method.

N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
  • `remove()` [prints a warning that its deprecated](https://stackoverflow.com/questions/46368368/mongodb-how-to-delete-all-records-of-a-collection-in-mongodb-shell#comment113856243_46368397) – Josiah Yoder May 27 '23 at 15:14
24

Here is the command to delete all the records of a collection in the MongoDB shell or MongoDB Compass shell (_MongoSH).

show dbs                                    -> to list all the database
use test-database                           -> to select the database
db.getCollection('orders').deleteMany({})   -> to select the collection and deletes all the data.

Or

show dbs                     -> to list all the database
use test-database            -> to select the database
db.orders.remove({})      -> to select the collection and deletes all the data.
Bathri Nathan
  • 1,101
  • 2
  • 13
  • 17
18

Use the deleteMany function (docs):

Assuming your collection named users:

db.users.deleteMany({})

deleteMany takes a filter as argument, and passing the empty document {} as the filter is equivalent to asking that all members of the collection be deleted.

Note that remove should still work, but you'll need to pass the same empty document:

db.users.remove({})

However, remove has been deprecated, and if your mongo install is up to date, using remove should lead to a deprecation warning indicating that you should instead use one of the preferred methods. From 1.0.7:

DeprecationWarning: Collection.remove() is deprecated. Use deleteOne, deleteMany, findOneAndDelete, or bulkWrite.

pb2q
  • 58,613
  • 19
  • 146
  • 147
10
db.users.count()
db.users.remove({})
db.users.count()
Omkesh Sajjanwar
  • 575
  • 8
  • 13
9

Delete all documents from a collection in cmd:

cd C:\Program Files\MongoDB\Server\4.2\bin
mongo
use yourdb
db.yourcollection.remove( { } )

This example is made for MongoDB 4.2

Uku Lele
  • 514
  • 1
  • 9
  • 14
7

To remove all the documents in all the collections:

db.getCollectionNames().forEach( function(collection_name) { 
    if (collection_name.indexOf("system.") == -1) {
        print ( ["Removing: ", db[collection_name].count({}), " documents from ", collection_name].join('') );
        db[collection_name].remove({}); 
    }
});
KalenGi
  • 1,766
  • 4
  • 25
  • 40
1

Please try the below code to remove all documents from all collections except starting from "system.".

use('DatabaseName');
db.getCollectionNames()
  .filter((collectionName) => collectionName.indexOf("system.") === -1)
  .forEach((collectionName) => {
    print(`Removing: ${db[collectionName].count({})} documents from ${collectionName}`);
    db[collectionName].deleteMany({});
  });

This code can be run in the MongoDB playground as well. It will also print the number of documents deleted per collection.

Rahul Mahadik
  • 794
  • 11
  • 19