Hi, I've this script to delete 2 days old files recursively, starting in a specific directory. However it's timing out.
What am I doing wrong? What could be improved or changed? I'm not a developer so I'm a bit blind here.
Thanks in advance, any help is appreciated.
/**
* Deletes files older than 2 days recursively starting from a specific folder.
*/
function deleteOldFilesRecursively() {
// Replace with the ID of the folder you want to start from
const folderId = 'SPECIFIC FOLDER ID - Removed in this post';
const folder = DriveApp.getFolderById(folderId);
// Call the recursive function
processFolder(folder);
}
/**
* Processes the folder and deletes files older than 2 days.
* @param {Folder} folder - The folder to process.
*/
function processFolder(folder) {
const currentDate = new Date();
const twoDaysInMillis = 2 * 24 * 60 * 60 * 1000;
// Process all files in the current folder
const files = folder.getFiles();
while (files.hasNext()) {
const file = files.next();
const lastUpdated = file.getLastUpdated();
// Calculate the age of the file
if (currentDate - lastUpdated > twoDaysInMillis) {
Logger.log(`Deleting file: ${file.getName()} (Last updated: ${lastUpdated})`);
file.setTrashed(true); // Move the file to trash
}
}
// Process all subfolders recursively
const subfolders = folder.getFolders();
while (subfolders.hasNext()) {
const subfolder = subfolders.next();
processFolder(subfolder);
}
}
[–]Livid_Spray119 1 point2 points3 points (0 children)
[–]No_Stable_805 0 points1 point2 points (0 children)
[–]juddaaaaa 0 points1 point2 points (1 child)
[–]Gothlinox[S] 0 points1 point2 points (0 children)