r/GoogleAppsScript • u/No_Season_5288 • May 11 '25
Question Repurposing a script
Hello!
I'm trying to adapt a script designed to automatically delete files from Google Drive to instead delete folders - this is the code (I have just replaced every reference to 'files' in the original code to 'folders' in this one)
function DeleteOldFolders() {
var Folders = new Array(
'183Pin7i9kow3fxwGt8k4fK4SnB70Xj-p',
'183Pin7i9kow3fxwGt8k4fK4SnB70Xj-p'
);
var Folders;
Logger.clear();
for (var key in Folders) {
Folder = DriveApp.getFolderById(Folders[key])
Folders = Folder.getFolders();
Logger.log('Opening Folder: ' + Folder.getName());
while (Folders.hasNext()) {
var Folder = Folders.next();
if (new Date() - Folder.getDateCreated() > 1 * 24 * 60 * 60 * 1000) {
Folder.setTrashed(true); // Places the Folder in the Trash folder
//Drive.Folders.remove(Folder.getId()); // Permanently deletes the Folder
Logger.log('Folder ' + Folder.getName() + ' was deleted.');
}
}
}
if(Logger.getLog() != '')
MailApp.sendEmail('tech@xxx.com', 'Backups have been removed from Google Drive', Logger.getLog());
}I keep encountering this error:
Error
Exception: Invalid argument: id
DeleteOldFolders
@ Copy of Code.gs:11I understand that the issue is a matter of recursively naming the variable, but I don't know how to correct line 11:
Folder = DriveApp.getFolderById(Folders[key])What can I change in order to get it to function?
1
Upvotes
View all comments
Show parent comment
1
u/WicketTheQuerent May 11 '25
If you will use Google Apps Script, you should spend some time learning programming using JavaScript. To do this, you could start by studying the content on https://developers.google.com/apps-script . It contains guides, quick starts and samples. This is good because you will learn through code snippets and examples created to work in Google Apps Script.
If you go to other places, look for one focusing on plain JavaScript. By this, I mean without using Web APIs like the DOM API, as most of them are unavailable in Google Apps Script because they run the code on Google servers, meaning there is a webpage / document as context.