Delete files from Google Drive folders after 7 days

 1 min read

Code Apps

function DeleteOldFiles() {
var Folders = new Array(
'FOLDER_ID_HERE', // My trash movies folder
'FOLDER_ID_HERE' // My backup servers folder
var Files;


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

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

  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', 'Oldest 7days 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.
  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. Add this script to Google Script by Then save it, test it and add permission as requested.
  6. To run this script once a day, go to

Inspired from

Tags: ,


Leave a Comment