MongoDB: Trade-offs of dropping a collection vs. removing all of its documents
If you go through a remove all the documents from a collection, then you'll be doing a lot more work (freeing the document's storage, clearing the index entries that point to the document, and so on). If you instead just drop the collection, it'll just be reclaiming the extents that the collection and its indexes use.
One other difference is that dropping the collection will also remove the collection's indexes.
A benefit of simply dropping a collection is that it is much faster than removing all of a collection's documents. If your collection will be "re-created immediately" anyway (assuming that includes index re-creation), then this is probably the most-attractive option.
The authors of the book MongoDB: The Definitive Guide (Kristina Chodorow and Michael Dirolf) ran an experiment where they provided a Python script which timed a drop
vs. a remove
of 1000000 records. The results came in at 0.01 seconds for the drop
and 46.08 seconds for the remove
. Now while the exact times may differ based-on hardware and other factors, it nonetheless illustrates the point that the drop
is significantly faster.
reference: Chodorow K., Dirolf M. (2010). “MongoDB: The Definitive Guide.” O'Reilly Media, Inc. Sebastapol, CA., pp.25