{"id":446,"date":"2013-11-11T00:21:20","date_gmt":"2013-11-11T00:21:20","guid":{"rendered":"http:\/\/www.theSQLReport.com\/?p=446"},"modified":"2016-02-20T12:58:55","modified_gmt":"2016-02-20T12:58:55","slug":"delete-or-drop-a-gridfs-object-in-mongodb","status":"publish","type":"post","link":"http:\/\/www.theSQLReport.com\/?p=446","title":{"rendered":"Delete or Drop a GridFS object in mongoDB"},"content":{"rendered":"<p>Recently I was asked to remove a GridFS object from one of our mongoDB databases.\u00a0 As of version 2.4.6 of mongoDB, this is not straight forward right click &amp; 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 <a title=\"MongoDB Desktop Application\" href=\"http:\/\/www.mongovue.com\/\" target=\"_blank\">MongoVUE<\/a> application).<\/p>\n<div id=\"attachment_447\" style=\"width: 659px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_01.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-447\" class=\"size-full wp-image-447 \" alt=\"As viewed in MongoVUE\" src=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_01.jpg\" width=\"649\" height=\"265\" srcset=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_01.jpg 649w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_01-300x122.jpg 300w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_01-210x85.jpg 210w\" sizes=\"(max-width: 649px) 100vw, 649px\" \/><\/a><p id=\"caption-attachment-447\" class=\"wp-caption-text\">As viewed in <a title=\"MongoDB Desktop Application\" href=\"http:\/\/www.mongovue.com\/\" target=\"_blank\">MongoVUE<\/a><\/p><\/div>\n<p>The <a title=\"GridFS\" href=\"http:\/\/docs.mongodb.org\/manual\/core\/gridfs\/\" target=\"_blank\">GridFS object<\/a> is a set of two collections.\u00a0 One of the collections has an extension &#8220;.files&#8221;, which stores the metadata for the files being stores.\u00a0 The other collection has an extension &#8220;.chunks&#8221;, which stores the files.\u00a0\u00a0 Pictured below is a screen shot of the mongo shell.\u00a0 I have connected to my_database, and ran the &#8220;show collections&#8221; command.\u00a0 In the example below, the GridFS object is the combination of the my_videos.chunks &amp; my_videos.files collections. \u00a0 The &#8220;_id&#8221;\u00a0 field in the document of the &#8220;.files&#8221; collection is linked to the &#8220;field_id&#8221; field of the document that stores the file of the &#8220;.chunks&#8221; collection with the save value.<\/p>\n<p style=\"text-align: left;\"><a href=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_02.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-448\" alt=\"Show Collections\" src=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_02.jpg\" width=\"779\" height=\"183\" srcset=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_02.jpg 779w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_02-300x70.jpg 300w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_02-210x49.jpg 210w\" sizes=\"(max-width: 779px) 100vw, 779px\" \/><\/a>To remove the GridFS object, both the &#8220;.files&#8221; collection &amp; &#8220;.chunks&#8221; collection need to be removed.\u00a0 Use the drop command to remove both collections in the mongo shell.\u00a0 Both collections need to be drop, but the order in which they are removed is not important.\u00a0 The files in the GridFS object do NOT need to be removed to drop the &#8220;.chunks&#8221; collection.\u00a0 Once the\u00a0&#8220;.chunks&#8221; collection has been dropped, the decrease size of the database will be reflected.\u00a0 Below I have used the syntax to drop the collection:<\/p>\n<p>db.&lt; collection name &gt;.drop( )<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_03.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-449\" alt=\"Drop Collections\" src=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_03.jpg\" width=\"281\" height=\"174\" srcset=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_03.jpg 281w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_03-210x130.jpg 210w\" sizes=\"(max-width: 281px) 100vw, 281px\" \/><\/a><\/p>\n<p>Below is the just a screen shot of all the command in the mongo shell to remove the my_videos GridFS object.\u00a0 The last &#8220;show collections&#8221; command shows that the my_videos GridFS object has been removed from the mongoDB database.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_04.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-450\" alt=\"Mongo Shell\" src=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_04.jpg\" width=\"370\" height=\"294\" srcset=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_04.jpg 370w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_04-300x238.jpg 300w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2013\/11\/MyGridFS_04-210x166.jpg 210w\" sizes=\"(max-width: 370px) 100vw, 370px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I was asked to remove a GridFS object from one of our mongoDB databases.\u00a0 As of version 2.4.6 of mongoDB, this is not straight forward right click &amp; delete &hellip; <a class=\"readmore\" href=\"http:\/\/www.theSQLReport.com\/?p=446\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":448,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-446","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts\/446"}],"collection":[{"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=446"}],"version-history":[{"count":16,"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts\/446\/revisions"}],"predecessor-version":[{"id":475,"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts\/446\/revisions\/475"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/media\/448"}],"wp:attachment":[{"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=446"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}