/* | |
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)); | |
} | |
} |
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.
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));
}
}
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));
}
}
How to add multiple email addresses to the list?
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!
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.
@Hectorhammett Indeed, @asifiqbal also mentions above encountering that limit.
@timur-tabi Thank you, I've added your suggestion to the gist above and linked to your Google Doc.