gridfs-stream icon indicating copy to clipboard operation
gridfs-stream copied to clipboard

Remove file does not delete chunks

Open slasktrat opened this issue 10 years ago • 8 comments

When i delete a file, all related chunks are left behind filling up storage space. Am I supposed to clear the chunks manually?

gfs.collection(req.params.bucket).remove({_id:file._id}, function (err) {
                if (err) {
                    return false;
                }
                return true;
            });

slasktrat avatar Aug 20 '15 10:08 slasktrat

I've managed to temporarily work around it by calling:

gfs.db.collection(req.params.bucket + '.chunks').remove({files_id:file._id}, function(err) { ...

slasktrat avatar Aug 20 '15 12:08 slasktrat

Same here! How to remove it if i only got gfs as object? What is in req.params.bucket?

mh4ck avatar Dec 08 '15 08:12 mh4ck

Name of the file bucket where you stored the file. If no name specified, it defaults to 'fs'

slasktrat avatar Dec 08 '15 08:12 slasktrat

Thank you! :+1

mh4ck avatar Dec 08 '15 08:12 mh4ck

Still not removing chunks... workaround works, but this is not pretty!

zevero avatar Mar 08 '17 14:03 zevero

I was having the same problems - stemmed from not passing the write db object to the Grid constructor when using mongoose.

I had some boiler plate express code that I had been using that looks like:

mongoose.connect(dbURL);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
   ...
   const gfs = Grid(db, mongoose.mongo);          // BAD!!!
   const gfs = Grid(db.db, mongoose.mongo);     // Terrible variable names but works
});

However my db variable was actually just a mongoose.connection object and thus I was passing that to the Grid constructor.

Using db.db (or better yet some sensible variable names solves the problem).

Maybe this will help someone else!

stefano-lupo avatar Nov 21 '17 13:11 stefano-lupo

adding the root option worked for me gfs.remove({_id: req.params.id, root: 'myFiles'}, function (err) {

pzemljic-git avatar Mar 28 '18 02:03 pzemljic-git

TypeError: gfs.deleteMany is not a function There is possible to use deleteMany concept in 'gridfs-stream'

rathipradha avatar May 19 '20 06:05 rathipradha