M
M
Mirtopir2021-05-06 22:52:36
Google Apps Script
Mirtopir, 2021-05-06 22:52:36

Apps script How to empty a folder?

Elementary code

function dellAllFile() {
  const tempFolderId = 'тут айди папки'//папка куда сохраняем временные файлы
  let files = DriveApp.getFolderById(tempFolderId).searchFiles('title != "nothing"');
  while (files.hasNext()) {
    let file = files.next();
    //Drive.Files.remove(file.getId());// вариант 1
    Drive.Files.trash(file.getId());// вариант 2
  }

Error:
GoogleJsonResponseException: Calling the drive.files.trash API method returned an error: Insufficient permissions for this file

What premise does it lack? Sometimes this deletes a couple of files, but then again falls out with an error.
Everything runs from one account. Folder owner = the owner of the script and the trigger is launched on his behalf.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ivanov, 2021-05-10
@oshliaer

Most likely you are deleting other people's files from someone else's folder. Log what is throwing the error

try {
  Drive.Files.trash(file.getId());
} catch (err) {
  console.log(err.message, file.getName());
}

Or exclude files you can't delete
let files = DriveApp.getFolderById(tempFolderId)
  .searchFiles('"me" in owners and title != "nothing"');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question