Skip to content

Instantly share code, notes, and snippets.

@adamloving
Last active December 12, 2019 07:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamloving/9630012 to your computer and use it in GitHub Desktop.
Save adamloving/9630012 to your computer and use it in GitHub Desktop.
Delete a bunch of emails (thousands) by tag from gmail
function batchDelete() {
var batchSize = 10; // Process up to 10 threads at once
for (i = 175; i > 0; i--) {
var threads = GmailApp.search('label:outbound older_than:' + i + 'd');
for (j = 0; j < threads.length; j += batchSize) {
GmailApp.moveThreadsToTrash(threads.slice(j, j+batchSize));
// Logger.log('finished move %d', j);
}
}
}
@adamloving
Copy link
Author

currently not working for me because...

Cannot connect to Gmail (line 5, file "batch-delete")

@otint
Copy link

otint commented Feb 24, 2015

works for me, had to authorize first

had a label with over 100,000 emails and no other solution worked, although the script times out after a few minutes it manages to delete a few thousand each time. I've slowly upped the batch size and so far that helps, haven't reached a batch size limit yet

function batchDelete() {
var batchSize = 100; // Process up to 10 threads at once
for (i = 175; i > 100; i--) {
var threads = GmailApp.search('label:outbound older_than:' + i + 'd');
for (j = 0; j < threads.length; j += batchSize) {
GmailApp.moveThreadsToTrash(threads.slice(j, j+batchSize));
//Logger.log('finished move %d', j);
}
}
}

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