I created a little script to delete files in specific folders older than 7 days on Google Drive, and if something is deleted it sends you an email! It doesn’t permanently delete them, it just puts them in the Trashed folder. It works quite well to delete files from Google Drive.

Note: If you’re looking for a backup script that works well with Google Drive, then look no further. I created a backup script that uses Google Drive as the storage solution. Here’s a backup script. It’s only made to work with RunCloud, but can be very easily modified to work on any Linux server.

I currently have a backup solution for my servers whereby my Google Drive structure looks like this:

  • Server Backups
    • Server 1
      • File 1
      • File 2
      • File 3
    • Server 35
      • File 1
      • File 2
      • File 3

The File 1, 2, or 3 can be MySQL backups, application data backups, etc.

Google Apps Script to delete files older than 7 Days in Google Drive

Chrome V8 Engine (Now enabled by default)

function DeleteOldFiles() {
  var Folders = new Array(
    'FOLDER_ID_HERE',
    'FOLDER_ID_HERE'
  );
  var Files;

  Logger.clear();

  for (var key in Folders) {
    Folder = DriveApp.getFolderById(Folders[key])
    Files = Folder.getFiles();
	
	Logger.log('Opening Folder: ' + Folder.getName());

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

      if (new Date() - File.getLastUpdated() > 7 * 24 * 60 * 60 * 1000) {
        File.setTrashed(true); // Places the file in the Trash folder
        //Drive.Files.remove(File.getId()); // Permanently deletes the file
        Logger.log('File ' + File.getName() + ' was deleted.');
      }
    }
  }

  if(Logger.getLog() != '')
    MailApp.sendEmail('YOUR_EMAIL_ADDRESS', 'Backups have been removed from Google Drive', Logger.getLog());
}

Old Runtime:

function DeleteOldFiles() {
  var Folders = new Array(
    'FOLDER_ID_HERE',
    'FOLDER_ID_HERE'
  );
  var Files;

  Logger.clear();

  for each (var FolderID in Folders) {
    Folder = DriveApp.getFolderById(FolderID)
    Files = Folder.getFiles();

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

      if (new Date() - File.getLastUpdated() > 7 * 24 * 60 * 60 * 1000) {
        File.setTrashed(true); // Places the file int the Trash folder
        //Drive.Files.remove(File.getId()); // Permanently deletes the file
        Logger.log('File ' + File.getName() + ' was deleted.');
      }
    }
  }

  if(Logger.getLog() != '')
    MailApp.sendEmail('YOUR_EMAIL_ADDRESS', 'Backups have been removed from Google Drive', Logger.getLog());
}

 

To complete the script, and delete files from Google Drive:

  1. Change FOLDER_ID_HERE with the folder ID’s you see in Google Drive. You can get them by double-clicking the folder and the URL structure will be something like this: https://drive.google.com/drive/folders/1Wx81C1hjghjBfgddfgfgmFeMfzdfgHyTdfgkLgdfg3yz
  2. If you have more folders, just keep adding the ID’s to the Array.
  3. Change YOUR_EMAIL_ADDRESS to your own email address to get notifications.
  4. If you want to permanently delete the files instead of placing them in the Trash folder then un-comment Drive.Files.remove(File.getId());
  5. The last part is quite easy and it requires you to make the script run once a day. While the Apps Script editor is open, in the menu click Edit -> All your triggers and then set a specific time.