简体   繁体   中英

Google apps script to clear folder given a folder_id not working (isTrashed not deleting files)

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();
  }
}

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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM