Truncate a collection

MongodbMongodb .Net-Driver

Mongodb Problem Overview


How do I truncate a collection in MongoDB or is there such a thing?

Right now I have to delete 6 large collections all at once and I'm stopping the server, deleting the database files and then recreating the database and the collections in it. Is there a way to delete the data and leave the collection as it is? The delete operation takes very long time. I have millions of entries in the collections.

Mongodb Solutions


Solution 1 - Mongodb

To truncate a collection and keep the indexes use

 db.<collection>.remove({})

Solution 2 - Mongodb

You can efficiently drop all data and indexes for a collection with db.collection.drop(). Dropping a collection with a large number of documents and/or indexes will be significantly more efficient than deleting all documents using db.collection.remove({}). The remove() method does the extra housekeeping of updating indexes as documents are deleted, and would be even slower in a replica set environment where the oplog would include entries for each document removed rather than a single collection drop command.

Example using the mongo shell:

var dbName = 'nukeme';
db.getSiblingDB(dbName).getCollectionNames().forEach(function(collName) {
	// Drop all collections except system ones (indexes/profile)
	if (!collName.startsWith("system.")) {
		// Safety hat
		print("WARNING: going to drop ["+dbName+"."+collName+"] in 5s .. hit Ctrl-C if you've changed your mind!");
		sleep(5000);
		db[collName].drop();
	}
})

It is worth noting that dropping a collection has different outcomes on storage usage depending on the configured storage engine:

  • WiredTiger (default storage engine in MongoDB 3.2 or newer) will free the space used by a dropped collection (and any associated indexes) once the drop completes.
  • MMAPv1 (default storage engine in MongoDB 3.0 and older) will not free up preallocated disk space. This may be fine for your use case; the free space is available for reuse when new data is inserted.

If you are instead dropping the database, you generally don't need to explicitly create the collections as they will be created as documents are inserted.

However, here is an example of dropping and recreating the database with the same collection names in the mongo shell:

var dbName = 'nukeme';

// Save the old collection names before dropping the DB
var oldNames = db.getSiblingDB(dbName).getCollectionNames();

// Safety hat
print("WARNING: going to drop ["+dbName+"] in 5s .. hit Ctrl-C if you've changed your mind!")
sleep(5000)

db.getSiblingDB(dbName).dropDatabase();

// Recreate database with the same collection names
oldNames.forEach(function(collName) {
	db.getSiblingDB(dbName).createCollection(collName);
})

Solution 3 - Mongodb

the below query will delete all records in a collections and will keep the collection as is,

db.collectionname.remove({})

Solution 4 - Mongodb

There is no equivalent to the "truncate" operation in MongoDB. You can either remove all documents, but it will have a complexity of O(n), or drop the collection, then the complexity will be O(1) but you will loose your indexes.

Solution 5 - Mongodb

Create the database and the collections and then backup the database to bson files using mongodump:

mongodump --db database-to-use

Then, when you need to drop the database and recreate the previous environment, just use mongorestore:

mongorestore --drop

The backup will be saved in the current working directory, in a folder named dump, when you use the command mongodump.

Solution 6 - Mongodb

remove() is deprecated in MongoDB 4.

You need to use deleteMany or other functions:

db.<collection>.deleteMany({})

Solution 7 - Mongodb

The db.drop() method obtains a write lock on the affected database and will block other operations until it has completed.

I think using the db.remove({}) method is better than db.drop().

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioniefpwView Question on Stackoverflow
Solution 1 - MongodbastroanuView Answer on Stackoverflow
Solution 2 - MongodbStennieView Answer on Stackoverflow
Solution 3 - MongodbIAmHomesView Answer on Stackoverflow
Solution 4 - MongodbAlonView Answer on Stackoverflow
Solution 5 - MongodbvinipsmakerView Answer on Stackoverflow
Solution 6 - MongodbChalistView Answer on Stackoverflow
Solution 7 - MongodbCongwu ZhangView Answer on Stackoverflow