I'm trying to delete files and folders older than 10 days in a specific Team Drive (not in the whole Google Drive).
To accomplish this I tried to merge what I read here
I tried to run the below script on Google app script. while running the script i am getting this error
Syntax error: SyntaxError: Unexpected identifier line: 10 file: Code.gs
I'm stuck with 'getFolderById' function.thats what in the line 10.
function deleteOldFiles() {
var Folders = new Array(
'YOUR-TEAM-DRIVE-ID' //you can find this in the team drive url
);
var DaysRetentionNumber = 15; //how many days old your files and folders must be before getting deleted?
var RetentionPeriod = DaysRetentionNumber * 24 * 60 * 60 * 1000;
Logger.clear();
for each (var FolderID in Folders) {
folder = DriveApp.getFolderById(FolderID);
processFolder(folder);
}
function processFolder(folder){
Logger.log('Folder: ' + folder.getName());
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
Logger.log('File: ' + file.getName());
if (new Date() - file.getLastUpdated() > RetentionPeriod) {
//file.setTrashed(true); //uncomment this line to put them in the trash
//Drive.Files.remove(file.getId()); //uncomment this line to delete them immediately; CAREFUL!
Logger.log('File '+ file.getName() + ' trashed');
}
}
var subfolders = folder.getFolders();
while (subfolders.hasNext()) {
subfolder = subfolders.next();
processFolder(subfolder);
}
checkEmptyFolder(folder);
}
function checkEmptyFolder(folder){
if(!folder.getFiles().hasNext() && !folder.getFolders().hasNext()){
Logger.log('Empty folder: '+ folder.getName());
folder.setTrashed(true); // put them in the trash
}
}
if(Logger.getLog() != '')
MailApp.sendEmail('youremailaddresshere', 'Team Drive weekly cleanup report', Logger.getLog()); //get a log in your email so that you can see what will be deleted; try this before uncommenting the trash/delete lines!
}
How can I modify this script to delete all team drive files older than 10 days ?
I tried this one too. but nothing worked for Google team drives
function delFilesInFolderOlderThanXday() {
const folder=DriveApp.getFolderById('id');
const files=folder.getFiles();
const dt=new Date();
const threshold=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()-X).valueOf();
while(files.hasNext()) {
let file=files.next();
let dtv=new Date(file.getLastUpdated()).valueOf();
if(dtv<threshold) {
Drive.Files.remove(file.getId(),{supportsAllDrives:true});
}
}
}
You will have to enable Drive API if you wish to delete. Otherwise you can put them in trash with DriveApp. You will also have to set X.
I tested this code as of Feb 4, 2022 and it is still working
Actually to be clear I tested this code because I just wanted to make sure that Drive.Files.remove() was still working:
function delFilesInFolderOlderThanXday() {
const folder = DriveApp.getFolderById('11wTZxbF9r-WlMK1uJlbcbmol7UcDV2X1');
const files = folder.getFiles();
const dt = new Date();
while (files.hasNext()) {
let file = files.next();
Drive.Files.remove(file.getId(), { supportsAllDrives: true });
}
}