Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ginoliang1130/a9023db3b92e7416bb1e799e46225cab to your computer and use it in GitHub Desktop.
Save ginoliang1130/a9023db3b92e7416bb1e799e46225cab to your computer and use it in GitHub Desktop.
A Google Apps Script script to bulk delete large amounts of email in Gmail while avoiding the error #793 which Gmail encounters normally
/*
This script, when used with Google Apps Scripts will delete 400 emails and
can be triggered to run every few minutes without user interaction enabling you
to bulk delete email in Gmail without getting the #793 error from Gmail.
Google returns a maximum of 500 threads. This limits to 400 threads in case 500
threads is causing timeouts
Configure the search query in the code below to match the type of emails
you want to delete
Browser to https://script.google.com/.
Start a script and paste in the code below.
After you past it in, save it and click the little clock looking button.
This is for your triggers. You can set up how frequently you want the script
to run (I did mine for every minute but others are seeing execution take longer than
a minute in which case you may want to run every 5 or 15 minutes).
This writeup from @timur-tabi goes into more detail : https://docs.google.com/document/d/1PLfAnNus-B87gHS1pkbmzFTkWckAPNcqmvO7hFo_gBc/edit
Source : # https://productforums.google.com/d/msg/gmail/YeQVDuPIQzA/kpZPDDj8TXkJ
*/
function batchDeleteEmail() {
var batchSize = 100 // Process up to 100 threads at once
var searchSize = 400 // Limit search result to a max of 400 threads. Use this if you encounter the "Exceeded maximum execution time" error
var threads = GmailApp.search('label:inbox from:user@example.com', 0, searchSize);
for (j = 0; j < threads.length; j+=batchSize) {
GmailApp.moveThreadsToTrash(threads.slice(j, j+batchSize));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment