I'm trying to clear an account of emails on Gmail. Doing it on the Gmail UI leads to errors probably because there are over 500k emails.
So I've started doing it with the Apps Script website https://script.google.com/ based on some scripts I have found and it works fine until around five or six minutes when it times out.
Right now the way I have it setup (code below), it runs fine but when it hits isTimeUp
it just exits. The newTrigger
is never running.
function myFunction() {
console.log("myFunction")
var batchSize = 100
var today = new Date();
while (true) {
var threads = GmailApp.search('before:2020/05/20');
for (j = 0; j < threads.length; j += batchSize) {
console.log("Batch " + j)
GmailApp.moveThreadsToTrash(threads.slice(j, j + batchSize));
}
if (isTimeUp(today)) {
console.log("time up")
var triggers = ScriptApp.getProjectTriggers();
try {
ScriptApp.deleteTrigger(triggers[0]);
} catch (e) {
}
ScriptApp.newTrigger("myFunction")
.timeBased()
.after(1000)
.create();
break;
}
}
}
function isTimeUp(today) {
var now = new Date();
return now.getTime() - today.getTime() > 300000;
}
This is the log I see:
4:46:05 PM Notice Execution started
4:46:05 PM Info myFunction
4:46:06 PM Info Batch 0
4:46:19 PM Info Batch 100
4:46:32 PM Info Batch 200
4:46:45 PM Info Batch 300
4:46:58 PM Info Batch 400
4:47:11 PM Info Batch 0
4:47:23 PM Info Batch 100
4:47:37 PM Info Batch 200
4:47:49 PM Info Batch 300
4:48:01 PM Info Batch 400
4:48:14 PM Info Batch 0
4:48:28 PM Info Batch 100
4:48:40 PM Info Batch 200
4:48:53 PM Info Batch 300
4:49:05 PM Info Batch 400
4:49:18 PM Info Batch 0
4:49:31 PM Info Batch 100
4:49:43 PM Info Batch 200
4:49:58 PM Info Batch 300
4:50:18 PM Info Batch 400
4:50:33 PM Info Batch 0
4:50:46 PM Info Batch 100
4:50:59 PM Info Batch 200
4:51:12 PM Info Batch 300
4:51:26 PM Info Batch 400
4:51:39 PM Info time up
4:51:39 PM Notice Execution completed
What am I doing wrong?