Delete or Drop a GridFS object in mongoDB

Recently I was asked to remove a GridFS object from one of our mongoDB databases.  As of version 2.4.6 of mongoDB, this is not straight forward right click & delete command. Below in my example, I am going to remove the my_videos GridFS object (below is a view of the GridFS object in the MongoVUE application).

As viewed in MongoVUE

As viewed in MongoVUE

The GridFS object is a set of two collections.  One of the collections has an extension “.files”, which stores the metadata for the files being stores.  The other collection has an extension “.chunks”, which stores the files.   Pictured below is a screen shot of the mongo shell.  I have connected to my_database, and ran the “show collections” command.  In the example below, the GridFS object is the combination of the my_videos.chunks & my_videos.files collections.   The “_id”  field in the document of the “.files” collection is linked to the “field_id” field of the document that stores the file of the “.chunks” collection with the save value.

Show CollectionsTo remove the GridFS object, both the “.files” collection & “.chunks” collection need to be removed.  Use the drop command to remove both collections in the mongo shell.  Both collections need to be drop, but the order in which they are removed is not important.  The files in the GridFS object do NOT need to be removed to drop the “.chunks” collection.  Once the “.chunks” collection has been dropped, the decrease size of the database will be reflected.  Below I have used the syntax to drop the collection:

db.< collection name >.drop( )

Drop Collections

Below is the just a screen shot of all the command in the mongo shell to remove the my_videos GridFS object.  The last “show collections” command shows that the my_videos GridFS object has been removed from the mongoDB database.

Mongo Shell

 

Categories: Linux

Comments are closed.