Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

56
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda