Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 email threads in a single API call.
This script fetches 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
See - https://developers.google.com/apps-script/reference/gmail/gmail-app#search(String)
and https://support.google.com/mail/answer/7190
Browse to https://script.google.com/
Start a script and paste in the code below.
After you paste it in, save it. In the drop down at the top select the function you want
to run. For example, you could run the batchDeleteEmail function.
This gist contains a few different functions to give examples of how to do other actions
besides deleting emails. For example if you wanted to mark all mail with the "work" label as read
you could run the markReadLabelWork function
Next 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
This gist includes additions by @kulemantu found in their fork : https://gist.github.com/kulemantu/84682cfebe72eb925cfe/revisions
*/
function batchDeleteEmail() {
processEmail('label:inbox from:user@example.com', 'moveThreadsToTrash');
}
function markReadLabelWork() {
processEmail('label:work', 'markThreadsRead');
}
function markReadFromInfoExample() {
processEmail('from:user@example.com', 'markThreadsRead');
}
function processEmail(search, batchAction) {
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(search, 0, searchSize);
for (j = 0; j < threads.length; j += batchSize) {
GmailApp[batchAction](threads.slice(j, j + batchSize));
}
}
@birajblg1987
Copy link

birajblg1987 commented Apr 24, 2022

How to add multiple email addresses to the list?

@Hectorhammett
Copy link

Hectorhammett commented Apr 25, 2022

I have an issue and I can't debug it. Is it me or the docs are a little bit stingy? Anyways, I cannot run the script as it says
Exception: Gmail Operation not allowed
On the line using GmailApp.search. Oddly enough, I left it running for something like an hour and it was working but then it stopped working. It sounds like a limit hit? but I am unsure as I am getting a Gmail operation not allowed and not something like limit error or something else. Any possible ideas?

Thank you!

@gene1wood
Copy link
Author

gene1wood commented Apr 25, 2022

How to add multiple email addresses to the list?

@birajblg1987 You can customize the GMail search terms on line 27 in any way you wish, including adding additional email addresses.

@gene1wood
Copy link
Author

gene1wood commented Apr 25, 2022

@Hectorhammett Indeed, @asifiqbal also mentions above encountering that limit.

@RenegadeMaster
Copy link

RenegadeMaster commented Aug 14, 2022

This script changed my life

@TheCosmonaut
Copy link

TheCosmonaut commented Oct 5, 2022

My script appears to be running but I don't see the overall count going down. Is there any code I can add which would allow me to log whether the threads were successfully removed?

@ethaniel
Copy link

ethaniel commented Nov 17, 2022

Thank you for an amazing script. However, in my case, I have around 200k messages that I need to delete. Here is how I altered the code to keep it running continuously:

function processEmail(search, batchAction) {
  var batchSize = 100; // Process up to 100 threads at once
  var searchSize = 500; // Limit search result to a max of 500 threads. (google doesn't allow more)
  do {
    var threads = GmailApp.search(search, 0, searchSize);
    console.log('processing ' + threads.length + ' threads');
    for (j = 0; j < threads.length; j += batchSize) {
      GmailApp[batchAction](threads.slice(j, j + batchSize));
    }
  } while (threads.length>0) // keep script running until there are no threads left
}

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