Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@adamloving adamloving commented Mar 18, 2014

currently not working for me because...

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

@otint

This comment has been minimized.

Copy link

@otint 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