S
S
SerJik_Z2022-02-18 15:10:36
Google Drive
SerJik_Z, 2022-02-18 15:10:36

Automatic deletion of files from all Google Drive folders using Google apps script?

Good afternoon! This script automatically deletes files older than n days from folders.

function DeleteOldBackup() {
  var Folders = new Array(
    'id папки',
  ' id папки'
    

  );
  var Files;
  Logger.clear();

  for (var key in Folders) {
    Folder = DriveApp.getFolderById(Folders[key])
    Files = Folder.getFiles();
  
  Logger.log('Открываю папку: ' + Folder.getName());

    while (Files.hasNext()) {
      var File = Files.next();

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


Let's say I have 200 folders, how can I make the script delete files older than n days without specifying all folders?

Answer the question

In order to leave comments, you need to log in

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

Наверное так - DriveApp.getFolders()
Но если нужно пробежаться по всем файлам, то я бы исключил из скрипта папки и напрямую посмотрел все файлы

let files = DriveApp.getFiles()

  while (files.hasNext()) {
    const file = files.next()
    Logger.log(file.getName())
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question