Home > Back-end >  Google apps script to clear folder given a folder_id not working (isTrashed not deleting files)
Google apps script to clear folder given a folder_id not working (isTrashed not deleting files)

Time:01-07

I am trying to write a google apps script to clear a given folder given the folder ID but the function is not behaving as expected. It runs without an error but it seems isTrashed() does not delete all files? I have looked at the documentation but can't seem to figure out how to get all the files in a folder to be deleted.

function clearFolder(folderId) {
  // Get the folder
  var folder = DriveApp.getFolderById(folderId);
  Logger.log("clearFolder on folder: %s",folder.getName())
  
  // Get all the files in the folder
  var files = folder.getFiles();

  // Iterate through the files and delete them
  while (files.hasNext()) {
    var file = files.next();
    Logger.log("clearFolder deleting file named '%s'",file.getName())
    file.isTrashed();
  }
}

CodePudding user response:

The file.isTrashed() method does not delete files. It determines whether the file is in the trash, and returns a true/false value that is ignored by the code you quote.

To delete files, use file.setTrashed(true).

  • Related