Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

javascript - Apps Script - Automatically Delete Files from Google Drive Older than 3 Days - Get List of Files

I'm using my Google Drive to store motion detection recordings from my IP cameras at home. I'm running the following script (slightly modified) to automatically delete files older than 3 days from my Google Drive:

Automatically delete files in google drive

The script is set to run every morning at around 5am. The problem I'm running into is, when there are too many files to delete, I get an error saying I've exceeded the max write rate. This is because the set trashed command is contained within a for-loop. If I add a sleep to slow down the loop, then I get another error saying the script didn't finish in the allowed time.

I could maybe just run it 2 or 3 times, but that's clunky.

How can I modify this script to have it create a temporary list of the files to be deleted, and when it finally steps out of the loop, execute the delete? I don't have much experience with GAS language and syntax, so specifics would be really awesome.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This code will return all the file ID's of files that are older than 30 days old. It is very different than the code referenced in the other post. The code that you are using has a deprecated class in it. DocsList is now deprecated. The code I'm giving you uses DriveApp, and makes use of the searchFiles() method.

Google Documentation - Search Files

gs Script

function getFilesByDate() {
  var arrayOfFileIDs = [];
  
  var ThirtyDaysBeforeNow = new Date().getTime()-3600*1000*24*30;
    // 30 is the number of days 
    //(3600 seconds = 1 hour, 1000 milliseconds = 1 second, 24 hours = 1 day and 30 days is the duration you wanted
    //needed in yr-month-day format
  
  var cutOffDate = new Date(ThirtyDaysBeforeNow);
  var cutOffDateAsString = Utilities.formatDate(cutOffDate, "GMT", "yyyy-MM-dd");
  //Logger.log(cutOffDateAsString);

  var theFileID = "";
  
  //Create an array of file ID's by date criteria
  var files = DriveApp.searchFiles(
     'modifiedDate < "' + cutOffDateAsString + '"');

  while (files.hasNext()) {
    var file = files.next();
    theFileID = file.getId();

    arrayOfFileIDs.push(theFileID);
    //Logger.log('theFileID: ' + theFileID);
    //Logger.log('date last updated: ' + file.getLastUpdated());
  }
  
  return arrayOfFileIDs;
  //Logger.log('arrayOfFileIDs: ' + arrayOfFileIDs);
};

You can un-comment the Logger.log() statements, run the code, and review what the code returns if you want. The following Logger.log() statement:

//Logger.log('date last updated: ' + file.getLastUpdated());

will print the dates of the files that were retrieved.

The above code doesn't delete any files, or set anything to trashed. It just searches files by date, and creates an array of all the file IDs.

So the function to delete or trash the files could first call that function and get a list of all the file ID's to work on.

It's a good idea to test the code without deleting any files first to make sure that the code is getting the correct files before running a version that deletes anything.

If you want to delete your files without first setting them to trashed, that can be done.

The example below uses the Advanced Google Drive Service. Refer to the documentation for the current way to enable the Advanced Drive Service.

//To delete a file without first sending it to the trash requires the Advanced Drive API to be enabled

function deleteFile(idToDLET) {
  idToDLET = 'the File ID';
  
  //This deletes a file without needing to move it to the trash
  var rtrnFromDLET = Drive.Files.remove(idToDLET);
}

To call the function that gets the file ID's to delete, it would look like this:

WARNING! THIS DELETES FILES WITHOUT SENDING THEM TO THE TRASH! **** MAKE SURE YOU KNOW WHAT IS BEING DELETED!

function deleteFiles() {
  var arrayIDs = getFilesByDate();
  
  for (var i=0; i < arrayIDs.length; i++) {
    Logger.log('arrayIDs[i]: ' + arrayIDs[i]);
    //This deletes a file without needing to move it to the trash
    var rtrnFromDLET = Drive.Files.remove(arrayIDs[i]);
  }
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...