How to delete a file using cfs:filesystem

The package is here, and I cannot find a remove/delete example on it.

I can upload a file to /public/uploads folder, and can obtain the file object like

_id: "5AqaDtx5baiLTssJX"
canRemove: true
createdAt: Fri Apr 24 2015 16:33:40 GMT+0800 (CST)
isPrivate: false
name: "EL.jpg"
url: "http://localhost:3000/uploads/files-WBLnuxBJazJ23dFzK-EL.jpg"

And intuitively tried something like Files.remove(file, function(err, file) {} but nothing goes on. I believe cfs:filesystem should have implemented the remove functionality, such as remove record from database, and delete the file on server.

So would anyone give me some examples on how to implement remove of a file? Thanks a lot!

1 Like

It should work like you mentioned, as long as your Files collection is an FS.Collection. Your document structure doesn’t look like a FS.Collection document, so are you storing your files in a seperate collection? If so you’re not using it correctly.
You should insert into an FS.Collection and put metadata in there, not store additional data in a separate collection. The main thing is that you don’t store the url itself, you let collectionfs do that for you with the .url() method.

If this is the case, best to read up on the documentation: https://github.com/CollectionFS/Meteor-CollectionFS, if not please provide some more info :wink:

orion.filesystem.providerUpload = function(options, success, failure) {
 
  var file = options.fileList[0];
 
  // use FS.Collection to upload the file
  Files.insert(file, function(err, file) {
    if (err) {
      console.log('error', err);
    } else {
      // recreate upload file pattern
      var fileName = file.collectionName + '-' + file._id + '-' + file.original.name;
      var fileUrl = Meteor.absoluteUrl() + upload_path + '/' + fileName;
  console.log(fileUrl);
      success(fileUrl);
    }
 
  });
}
 
orion.filesystem.providerRemove = function(file, sucess, failure) {
  console.log('delete file');
  console.log(file);
  
  Files.remove(file, function(err, file) {
    if (err) {
      console.log('error', err);
    };
  });
}

The code is like above, I guess I should find the FS.File by file._id? and then remove the FS.file instance?

Oh, the FS file id is in the file name. The provider uses another collection to store file.

Exactly, if you remove the FS.file, collectionfs will remove the file from the server.

It seems you’re working on someone else his code, so you might not be able to change too much, but I would advise you to at least include the id to the FS.file instance in the other collection, that would make things a lot simpler to reference, like:

_id: "5AqaDtx5baiLTssJX",
canRemove: true,
createdAt: Fri Apr 24 2015 16:33:40 GMT+0800 (CST),
isPrivate: false,
name: "EL.jpg",
url: "http://localhost:3000/uploads/files-WBLnuxBJazJ23dFzK-EL.jpg",
fileID: "the ObjectID of the FS.file"

Yeah, I’ve done that. Although the code is not so good, but works. Thanks a lot!

f = Files.findOne(cfs_id);

Files.remove(f, function(err, file) {
    if (err) {
      console.log('error', err);
    } else {
      console.log('remove success');
      success();
    };
  });

Oh, I still have some bugs here. Sometimes it successfully removes the file on the server, sometimes doesn’t.

And findOne may occur undefined. Why’s that? the cfs_id is right? Is that Files does not have findOne method? Thanks?

  var f = Files.findOne(cfs_id);
  console.log(f); // undefined

If findOne returns undefined then it’s because it can’t find the record with the query you gave (all collections have the findOne() function), this could be a problem with numerous things.

Most probable are that cfs_id isn’t the correct one, or you simply don’t have that file in your collection at that moment (publish/subscribe hasn’t returned the data or something along those lines).

This will probably be the same problem that’s causing not all files being deleted as I personally have not encountered issues with removing on collectionfs.

I wrote the files in package. Is that a problem related to package?

I am 90% sure that cfs_id is the right one, I’ve checked that in robomongo.

In cfs.files.filerecord, the cfs_id is the same as record_id.

To remove a file when using CFS you should be able to call the remove method provided by the File class.

Files.findOne(fileId).remove();

To call this from the client you’ll need to setup your allow or deny rules on the server

2 Likes