Skip to content

Instantly share code, notes, and snippets.

@yarko
Forked from gene1wood/batch-delete-gmail-emails.js
Last active December 12, 2020 14:53
Show Gist options
  • Save yarko/781f3736a7176277d1baf142b780b290 to your computer and use it in GitHub Desktop.
Save yarko/781f3736a7176277d1baf142b780b290 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 500 emails - the amount the API will return with one call.
# It can be triggered to run periodically (I set @ 5 minute, since it runs slow) without user interaction
# enabling you to bulk delete email in Gmail without getting the #793 errors, etc., from Gmail.
# 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?hl=en
# 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 every 5 min).
# Source : # https://productforums.google.com/d/msg/gmail/YeQVDuPIQzA/kpZPDDj8TXkJ
# I broke apart the for-loop of the original, so I could debug - the search was returning nothing for
# me for a while. This explicit form is easier to step through and see where the problems are.
function batchDeleteEmail() {
var batchSize = 100; // Process up to 100 threads at once
var threads = GmailApp.search('label:+Github_Activities');
var j = 0;
var l = threads.length;
while ( j < l ) {
GmailApp.moveThreadsToTrash(threads.slice(j, j+batchSize));
j+=batchSize;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment