Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

54
Views
Infinite Loop for cleaning

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));
}
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!