I'm trying to delete files and folders older than 7 days in a specific Team Drive (not in the whole Google Drive).
To accomplish this I tried to merge what I read here: Apps Script - Automatically Delete Files from Google Drive Older than 3 Days - Get List of Files
I don't have enough rep to add a comment there so that's why I'm opening a new thread. Here you can find my ripoff from user1588938:
function getOldFileIDs() {
var fileIDs = [];
// Old date is 30 days
var oldDate = new Date().getTime() - 3600*1000*24*30;
var cutOffDate = Utilities.formatDate(new Date(oldDate), "GMT", "yyyy-MM-dd");
// Get folderID using the URL on google drive
var folder = DriveApp.getFolderById('XXXXXXX');
var files = folder.searchFiles('modifiedDate < "' + cutOffDate + '"');
while (files.hasNext()) {
var file = files.next();
fileIDs.push(file.getId());
Logger.log('ID: ' + file.getId() + ', Name: ' + file.getName());
}
return fileIDs;
};
function deleteFiles() {
var fileIDs = getOldFileIDs();
fileIDs.forEach(function(fileID) {
DriveApp.getFileById(fileID).setTrashed(true);
});
};
I'm stuck with 'getFolderById' function because I suppose it doesn't apply to a Team Drive root but only works for folders inside of it. Indeed, when I look at the logs I can see that the output for:
var folder = DriveApp.getFolderById('this-is-my-team-drive-id');
is a generic: [18-07-30 06:34:49:146 PDT] Team Drive and not the name of the Team Drive I chose.
I can't go any further with the script because of this.
Any hint on how to list every file and subfolder in a Team Drive using searchFiles?
This solution might apply but it works for a folder inside a Team Drive and not on the root of the Team Drive: browse files in google team drive
Thanks!