Skip to content

Instantly share code, notes, and snippets.

@JoniWeiss
Created November 14, 2021 17:50
Show Gist options
  • Save JoniWeiss/1f42b6fdd2cd8a8f85ee33004090bd4f to your computer and use it in GitHub Desktop.
Save JoniWeiss/1f42b6fdd2cd8a8f85ee33004090bd4f to your computer and use it in GitHub Desktop.
Google Apps Script - Delete old files from a folder in google drive
//
// From source: https://blog.ss88.us/delete-files-from-google-drive-folders-after-x-days-apps-script
//
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());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment