Last active
January 7, 2024 12:07
-
-
Save shoelaced/becaed67f443268166f07edc3f936bdc to your computer and use it in GitHub Desktop.
Google Script: Auto-deletes old files in Google Drive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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; | |
| } |
Hi there, @TENSytems -
I've updated the gist and made a couple of other improvements. Empty folders should now also be deleted, but this can be turned off in the SETTINGS variable. Please note that this checks folders recursively and there are limits for app execution time - thus this script might timeout if you have a lot of files and folders, and perhaps require multiple runs to complete.
Please give it a try and let me know how you get on, but I would recommend testing it on another empty Google Drive account first. I did a basic test but I don't know your use case.
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
Thank you for this great script which I now successfully use.

Is there a way how to also automatically delete the empty folders where the files were deleted?
Thx