Search code examples
google-apps-scriptgoogle-drive-api

Adjust script that deletes files older than 30 days on Google Drive


I have this script that deletes old files from Google Drive ... I would like to adjust it so that it deletes files from a specific folder, specifically files that were created over 3 hours ago. And one more detail, the folder cannot be empty, so the latest file even if it has been published for more than 3 hours cannot be deleted. It can only be deleted when another file is added in the folder. I need help getting this done, I can't edit the script for these details.

function getOldFileIDs() {
  var fileIDs = [];
  // Old date is 30 days
  var oldDate = new Date().getTime() - 3600*1000*24*30;
  var cutOffDate = Utilities.formatDate(new Date(oldDate), "GMT", "yyyy-MM-dd");

  // Get folderID using the URL on google drive
  var folder = DriveApp.getFolderById('XXXXXXX');
  var files = folder.searchFiles('modifiedDate < "' + cutOffDate + '"');

  while (files.hasNext()) {
    var file = files.next();
    fileIDs.push(file.getId());
    Logger.log('ID: ' + file.getId() + ', Name: ' + file.getName());
  }
  return fileIDs;
};

function deleteFiles() {
  var fileIDs = getOldFileIDs();
  fileIDs.forEach(function(fileID) {
    DriveApp.getFileById(fileID).setTrashed(true);
  });
};

Solution

    • You want to delete the files which are modifiedDate < 3 hours.
    • There are the files in the specific folder.
    • You want to achieve this using Google Apps Script.

    If my understanding is correct, how about this modification?

    Modification point:

    • When the document of Search for Files is seen, for modifiedDate, it seems that the ISO format is required to be used.

    When above point and modifiedDate < 3 hours are reflected to your script, it becomes as follows.

    Modified script:

    When your script is modified, please modify as follows.

    From:
    var oldDate = new Date().getTime() - 3600*1000*24*30;
    var cutOffDate = Utilities.formatDate(new Date(oldDate), "GMT", "yyyy-MM-dd");
    

    To:

    var oldDate = new Date().getTime() - 3600*1000*3;
    var cutOffDate = new Date(oldDate).toISOString();
    

    Reference:

    If I misunderstood your question and this was not the result you want, I apologize.

    Added 1:

    • You want to delete the files which are modifiedDate < 3 hours.
    • There are the files in the specific folder.
    • You want to achieve this using Google Apps Script.
    • You want to leave the newest created file.

    If my understanding is correct, how about this modification?

    Modified script:

    Please modify the function of getOldFileIDs() as follows.

    function getOldFileIDs() {
      // Old date is 30 days
      var oldDate = new Date().getTime() - 3600*1000*3;
      var cutOffDate = new Date(oldDate).toISOString();
    
      // Get folderID using the URL on google drive
      var folder = DriveApp.getFolderById('XXXXXXX');
      var files = folder.searchFiles('modifiedDate < "' + cutOffDate + '"');
    
      var obj = [];
      while (files.hasNext()) {
        var file = files.next();
        obj.push({id: file.getId(), date: file.getDateCreated()});
      }
      obj.sort(function(x, y) {return x.date < y.date ? 1 : -1});
      obj.shift();
      var fileIDs = obj.map(function(e) {return e.id});
      return fileIDs;
    };
    
    • In this modified script, the retrieved files are sorted by the date and the top of element of the array is deleted. By this, the file IDs except for the newest created file are returned.

    Added 2:

    • You want to completely delete the files without moving to trashbox.

    If my understanding is correct, how about this modification?

    Modified script:

    Please modify the function of deleteFiles() as follows. Before you run the script, please enable Drive API at Advanced Google services.

    From:
    DriveApp.getFileById(fileID).setTrashed(true);
    
    To:
    Drive.Files.remove(fileId);
    
    • By this modification, the file ID is completely deleted. So when you test this, please be careful.