Skip to content

Instantly share code, notes, and snippets.

@shoelaced
Last active December 24, 2025 10:36
Show Gist options
  • Select an option

  • Save shoelaced/becaed67f443268166f07edc3f936bdc to your computer and use it in GitHub Desktop.

Select an option

Save shoelaced/becaed67f443268166f07edc3f936bdc to your computer and use it in GitHub Desktop.
Google Script: Auto-deletes old files in Google Drive
/**
* Google Apps Script to delete old files and optionally empty folders in Google Drive.
*
* Settings:
* - cutOffDays: Number of days before the current date to consider for file deletion.
* - deleteEmptyFolders: Set to true to delete empty directories, false to keep them.
*
* Instructions:
* 1. Ensure "Drive" advanced service is enabled:
* - In the Google Apps Script dashboard, click "+ Services" in the sidebar.
* - Find "Drive API" in the list and click it.
* - Click "Add" to enable it. Ensure the API version is set to "v2".
* 2. To schedule this script:
* - Click the clock icon on the sidebar in the Google Apps Script dashboard.
* - Click the '+' to add a new trigger.
* - Set function to "deleteFiles", event source to "Time-driven", and choose frequency.
* - Save the trigger. Grant necessary permissions for the script to run.
*
* Note: Be aware of Google Apps Script's execution time limits. For large and deeply nested
* folder structures, the script may take considerable time and potentially hit these limits.
*/
// Settings
var SETTINGS = {
cutOffDays: 8, // Number of days before the current date to consider for file deletion
deleteEmptyFolders: true // Set to true to delete empty directories, false to keep them
};
function getFilesByDate() {
var arrayOfFileIDs = [];
var scriptId = ScriptApp.getScriptId(); // Get the current script's ID
// Calculate the cutoff date based on settings
var cutOffDate = new Date();
cutOffDate.setDate(cutOffDate.getDate() - SETTINGS.cutOffDays);
var cutOffDateAsString = Utilities.formatDate(cutOffDate, "GMT", "yyyy-MM-dd");
var files = DriveApp.searchFiles(
'modifiedDate < "' + cutOffDateAsString + '"'
);
while (files.hasNext()) {
var file = files.next();
if (file.getId() !== scriptId) { // Exclude the script itself
arrayOfFileIDs.push(file.getId());
}
}
return arrayOfFileIDs;
}
function deleteFiles() {
try {
var arrayIDs = getFilesByDate();
for (var i = 0; i < arrayIDs.length; i++) {
Drive.Files.remove(arrayIDs[i]);
}
if (SETTINGS.deleteEmptyFolders) {
deleteEmptyFolders();
}
} catch (error) {
Logger.log("An error occurred: " + error.toString());
}
}
function deleteEmptyFolders() {
var rootFolders = DriveApp.getFolders();
while (rootFolders.hasNext()) {
var folder = rootFolders.next();
checkAndDeleteFolder(folder);
}
}
function checkAndDeleteFolder(folder) {
var files = folder.getFiles();
var subfolders = folder.getFolders();
var isEmpty = true;
// Check if there are any files in the folder
if (files.hasNext()) {
return false; // Folder is not empty
}
// Recursively check each subfolder
while (subfolders.hasNext()) {
var subfolder = subfolders.next();
if (!checkAndDeleteFolder(subfolder)) {
isEmpty = false; // Contains non-empty subfolder
}
}
// If the folder is empty, delete it
if (isEmpty) {
folder.setTrashed(true); // Move the folder to trash
}
return isEmpty;
}
@TENSytems
Copy link

TENSytems commented Jan 5, 2024

Hi shoelaced,
Thanks a lot for your work. The script works perfect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment