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));
}
}
@asifiqbal
Copy link

asifiqbal commented Aug 8, 2021

@timur-tabi
Copy link

timur-tabi commented Aug 8, 2021

Try this version. It limits to 400 threads:

function batchDeleteEmail() 
{
  // moveThreadsToTrash() only allows up to 100 threads at once
  var batchSize = 100

  // returns no more than 500 results, but that takes longer than
  // six minutes, so return less.  We don't want timeouts.
  var threads = GmailApp.search('label:inbox from:user@example.com', 0, 400);
  
  for (j = 0; j < threads.length; j += batchSize) {
    GmailApp.moveThreadsToTrash(threads.slice(j, j + batchSize));
  }
}

I suggest running this script not more often than once per hour.

@asifiqbal
Copy link

asifiqbal commented Aug 8, 2021

Try this version. It limits to 400 threads:

function batchDeleteEmail() 
{
  // moveThreadsToTrash() only allows up to 100 threads at once
  var batchSize = 100

  // returns no more than 500 results, but that takes longer than
  // six minutes, so return less.  We don't want timeouts.
  var threads = GmailApp.search('label:inbox from:user@example.com', 0, 400);
  
  for (j = 0; j < threads.length; j += batchSize) {
    GmailApp.moveThreadsToTrash(threads.slice(j, j + batchSize));
  }
}

I suggest running this script not more often than once per hour.

That worked perfect. Now I just setup a trigger to run hourly. Does it start at the beginning of the hour? There is nothing under Last run yet
Screen Shot 2021-08-08 at 6 09 49 PM

@asifiqbal
Copy link

asifiqbal commented Aug 8, 2021

Try this version. It limits to 400 threads:

function batchDeleteEmail() 
{
  // moveThreadsToTrash() only allows up to 100 threads at once
  var batchSize = 100

  // returns no more than 500 results, but that takes longer than
  // six minutes, so return less.  We don't want timeouts.
  var threads = GmailApp.search('label:inbox from:user@example.com', 0, 400);
  
  for (j = 0; j < threads.length; j += batchSize) {
    GmailApp.moveThreadsToTrash(threads.slice(j, j + batchSize));
  }
}

I suggest running this script not more often than once per hour.

That worked perfect. Now I just setup a trigger to run hourly. Does it start at the beginning of the hour? There is nothing under Last run yet
Screen Shot 2021-08-08 at 6 09 49 PM

I changed it to run every 15 mins and I do see it running. I will change it back to run it every hour. Thanks!

@gene1wood
Copy link
Author

gene1wood commented Aug 8, 2021

@timur-tabi Thank you, I've added your suggestion to the gist above and linked to your Google Doc.

@asifiqbal
Copy link

asifiqbal commented Aug 19, 2021

So the script has been working fine for most times (trigger is setup for one per hr) with searchSize = 400. However I still get the Exceeded maximum execution time once in a while and sometimes says Exception: Gmail operation not allowed. Out of 24 a day (one/hr) I get 3 failed with gmail operation not allowed and 3 timedout with execeeded max execution time. 75% success rate (yikes). Google GMAIL api is pretty unstable seems like.

@ginoliang1130
Copy link

ginoliang1130 commented Aug 30, 2021

Thank you. This script is very helpful to me. I release a new version to everyone. By the way, I added Mandarin in the comments. Hope it helps others.

function batchDeleteEmail() {
  var batchSize = 100 // Process up to 100 threads at once
  // 如果需要時間可以加上 [older_than:00m、00d、00h],[category:] 可換成 [label:]
  // If you need to add time conditions, you can use d(day) and h(hours) instead.
  // [category:] can be replace [label:].
  var threads = GmailApp.search('category:forums');
  console.log('batch size is: ' + threads.length);
  for (j = 0; j < threads.length; j+=batchSize) {
    console.log('removing batch: ' + j);
    GmailApp.moveThreadsToTrash(threads.slice(j, j+batchSize));
  }
}

@aalemayhu
Copy link

aalemayhu commented Nov 6, 2021

Thank you so much! For anyone else wanting to delete patches, this is my

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('subject: PATCH', 0, searchSize);
  for (j = 0; j < threads.length; j+=batchSize) {
    GmailApp.moveThreadsToTrash(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

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