Skip to content

Instantly share code, notes, and snippets.

@apostololeg
Last active November 4, 2021 13:46
Show Gist options
  • Save apostololeg/6b7af65e50f778e5b878a916dc238b02 to your computer and use it in GitHub Desktop.
Save apostololeg/6b7af65e50f778e5b878a916dc238b02 to your computer and use it in GitHub Desktop.
Gmail remove batch messages
let count = 1500;
const batchSize = 100;
async function purgeGmail(query) {
const items = GmailApp.search("label:github", 0, batchSize);
console.log(count, items.length);
await GmailApp.moveThreadsToTrash(items);
count -= batchSize;
if (count > 0) purgeGmail();
}
let count = 32000;
const step = 1500;
const delay = 1000;
const getRunBtn = () => document.querySelector('[aria-label="Run the selected function"]');
function resolveWhenBtnEnables(resolve) {
function check() {
if (getRunBtn().disabled) setTimeout(check, delay);
else resolve();
}
setTimeout(check, delay);
}
function waitForRunBtn() {
return new Promise(resolveWhenBtnEnables);
}
function run() {
console.log('run', count);
getRunBtn().click();
count -= step;
if (count > 0) waitForRunBtn().then(run);
}
run();
@apostololeg
Copy link
Author

apostololeg commented Nov 4, 2021

Gmail has limitations for batch operations, so i wrote a little script for it:

  1. go to https://script.google.com/
  2. create new project
  3. paste Code.gs (update search filter)
  4. open devTool console
  5. paste devTool_console.js (update variable count)
  6. press Enter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment