I'm looking to loop this to clean out ~1000000 emails any help appreciated!
function batchDeleteA() {
var batchSize = 100 // Process up to 100 threads at once
var threads = GmailApp.search('label:inbox older_than:2d');
for (j = 0; j < threads.length; j+=batchSize) {
GmailApp.moveThreadsToTrash(threads.slice(j, j+batchSize));
}
}
If it is a large loop, you probably want to use a queue system where you use timeouts to do the loop. Basic idea below with hard coded array.
function batchDeleteA() {
//var batchSize = 100;
var batchSize = 3;
// var threads = GmailApp.search('label:inbox older_than:2d');
var threads = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var removeBatch = function() {
var batch = threads.splice(0, Math.min(batchSize, threads.length));
// GmailApp.moveThreadsToTrash(batch);
if (threads.length) {
console.log("remaining: ", threads.length);
window.setTimeout(removeBatch, 1);
} else {
console.log("complete");
}
};
removeBatch();
}
batchDeleteA()
If you are getting throttled, you can increase the timeout from 1 to a larger number of milliseconds.