Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading