Last active
December 16, 2024 07:38
-
-
Save gene1wood/0f455239490e5342fa49 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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. | |
Now you need to set the authorized scopes for the script ( https://developers.google.com/apps-script/concepts/scopes ) | |
On the left side of the screen, hover over the gear symbol and then click on Project Settings. | |
Enable 'Show "appsscript.json" manifest file in editor' | |
On the left side of the screen, click on the < > to return to the code editor | |
You should see a new file appear, appscript.json. Click on it. | |
Edit the file to add an oauthScopes entry for mail.google.com. It should look something like this. | |
{ | |
"oauthScopes": [ | |
"https://mail.google.com/", | |
"https://www.googleapis.com/auth/gmail.readonly", | |
"https://www.googleapis.com/auth/gmail.modify" | |
], | |
"timeZone": "America/New_York", | |
"dependencies": { | |
"enabledAdvancedServices": [ | |
{ | |
"userSymbol": "Gmail", | |
"version": "v1", | |
"serviceId": "gmail" | |
} | |
] | |
}, | |
"exceptionLogging": "STACKDRIVER", | |
"runtimeVersion": "V8" | |
} | |
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)); | |
} | |
} |
Note: GMAIL has a script execution limit of 6 minutes (not sure) and daily limits (positive).
function deleteAllEmailsContinuouslyEnhanced() {
var batchSize = 20; // Number of threads per batch
var maxExecutionTime = 300000; // 5 minutes (300,000 ms)
var startTime = new Date().getTime(); // Track overall start time
var batchStartTime, batchEndTime, batchDuration; // Variables for batch timing
var totalEmailsProcessed = 0; // Track total emails processed
var totalTimeSpent = 0; // Track cumulative time spent processing
while (new Date().getTime() - startTime < maxExecutionTime) {
batchStartTime = new Date().getTime(); // Record batch start time
// Fetch threads not in Trash
var threads = GmailApp.search("-in:trash", 0, batchSize);
if (threads.length === 0) {
Logger.log("All emails processed.");
Logger.log("Total emails processed: " + totalEmailsProcessed);
Logger.log("Total time spent: " + formatTime(totalTimeSpent));
Logger.log("Average time per batch: " + formatTime(totalTimeSpent / totalEmailsProcessed * batchSize));
return; // Stop if no more threads to process
}
// Move threads to Trash
GmailApp.moveThreadsToTrash(threads);
// Record batch end time and calculate duration
batchEndTime = new Date().getTime();
batchDuration = batchEndTime - batchStartTime; // Duration in milliseconds
// Update stats
totalEmailsProcessed += threads.length;
totalTimeSpent += batchDuration;
// Log details for the batch
Logger.log(
threads.length +
" threads moved to Trash. Time taken for this batch: " +
formatTime(batchDuration)
);
}
// Final stats when time limit is reached
Logger.log("Execution time limit reached. Run script again to continue.");
Logger.log("Total emails processed: " + totalEmailsProcessed);
Logger.log("Total time spent: " + formatTime(totalTimeSpent));
Logger.log("Average time per batch: " + formatTime(totalTimeSpent / totalEmailsProcessed * batchSize));
}
// Helper function to format time into human-readable format
function formatTime(milliseconds) {
var totalSeconds = Math.floor(milliseconds / 1000);
var hours = Math.floor(totalSeconds / 3600);
var minutes = Math.floor((totalSeconds % 3600) / 60);
var seconds = totalSeconds % 60;
return (hours > 0 ? hours + "h " : "") +
(minutes > 0 ? minutes + "m " : "") +
seconds + "s";
}
Execution log
12:08:04 PM Notice Execution started
12:08:05 PM Info 🔄 Starting to delete all emails...
12:08:39 PM Info 🗑️ Processed 100 emails in this batch. Time taken: 33s
12:09:17 PM Info 🗑️ Processed 100 emails in this batch. Time taken: 37s
12:09:52 PM Info 🗑️ Processed 100 emails in this batch. Time taken: 34s
12:10:26 PM Info 🗑️ Processed 100 emails in this batch. Time taken: 34s
12:11:01 PM Info 🗑️ Processed 100 emails in this batch. Time taken: 33s
12:11:35 PM Info 🗑️ Processed 100 emails in this batch. Time taken: 34s
12:12:10 PM Info 🗑️ Processed 100 emails in this batch. Time taken: 34s
12:12:44 PM Info 🗑️ Processed 100 emails in this batch. Time taken: 33s
12:13:18 PM Info 🗑️ Processed 100 emails in this batch. Time taken: 33s
12:13:19 PM Info ⏳ Execution time limit reached. Run the script again to continue.
12:13:19 PM Info 📊 Summary:
12:13:19 PM Info - Total emails processed: 900
12:13:19 PM Info - Total time spent: 5m 9s
12:13:19 PM Info - Average time per batch: 34s
12:13:18 PM Notice Execution completed
Great things,
a simple modification to delete all unread without attachment.
function batchDeleteEmail()
{
var batchSize = 100
var threads = GmailApp.search('label:inbox is:unread -has:attachment', 0, 400);
for (i = 0; i < threads.length; i += batchSize) {
GmailApp.moveThreadsToTrash(threads.slice(i, i + batchSize));
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you all for sharing your scripts! They have been super helpful.
I just wanted to mention that there seems to be a daily limitation now on how many times a script can be ran per day. I canceled some of the simultaneous scripts in the beginning when testing things out, so I'm not exactly sure if some of the canceled scripts are counted in the daily total or not, but the limit is somewhere between 40 to 50 scripts per day.