Truncate a collection

iefpw picture iefpw · May 11, 2013 · Viewed 73.4k times · Source

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.

Answer

astroanu picture astroanu · Apr 23, 2014

To truncate a collection and keep the indexes use

 db.<collection>.remove({})