S
S
SerJik_Z2022-02-23 12:03:25
JavaScript
SerJik_Z, 2022-02-23 12:03:25

Delete files only from folders older than n days in Google drive?

Good afternoon! This script looks at all files older than n days and deletes them.

function DeleteOldBackup() {
    let Files = DriveApp.getFiles()

      while (Files.hasNext()) {
        const File = Files.next()
        Logger.log(File.getName())
     
          if (new Date() - File.getLastUpdated() > 25 * 24 * 3600 * 1000) {
            File.setTrashed(true); 
            Drive.Files.remove(File.getId()); 
            Logger.log('File ' + File.getName() + ' удалено');
          }
        }
      }


How can I make it delete files only in folders? It is not an option to indicate the folder ID, there are more than 300 folders over time, they are added and removed.
This script, which is not suitable now, has files that are shared and they are not in folders, but this script does not delete, as it reaches them, the script stops, since I am not the owner of the file. Tried through the try...catch construct, didn't work.
Tell me how to do it through DriveApp.getFolders() without specifying the folder id.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2022-02-23
@SerJik_Z

Have a look at getParents()
https://developers.google.com/apps-script/referenc...
I think this should help the question.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question