Forked from gene1wood/batch-delete-gmail-emails.js
Last active
July 28, 2025 00:40
-
-
Save maxugly/5847f8dd908bdfde94e2e4047d6e5a12 to your computer and use it in GitHub Desktop.
Revisions
-
maxugly revised this gist
Jul 28, 2025 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -96,7 +96,7 @@ a verbose version function batchDeleteEmail() { console.log('Starting batch email deletion process'); var searchQuery = 'subject:twitch'; console.log('Search query:', searchQuery); processEmail(searchQuery, 'moveThreadsToTrash'); -
maxugly revised this gist
Jul 28, 2025 . 1 changed file with 88 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -88,4 +88,92 @@ function processEmail(search, batchAction) { for (j = 0; j < threads.length; j += batchSize) { GmailApp[batchAction](threads.slice(j, j + batchSize)); } } /* a verbose version */ function batchDeleteEmail() { console.log('Starting batch email deletion process'); var searchQuery = 'subject:yt-dlp OR ggerganov OR ggml OR Twitch OR quora OR spotify OR devry OR "medium daily digest" OR zdnet OR starbucks OR Hyundai OR fortunejack'; console.log('Search query:', searchQuery); processEmail(searchQuery, 'moveThreadsToTrash'); console.log('Batch email deletion process completed'); } 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 console.log('Starting processEmail function'); console.log('Search query:', search); console.log('Batch action:', batchAction); console.log('Batch size:', batchSize); console.log('Search size limit:', searchSize); var startTime = new Date(); console.log('Search started at:', startTime); var threads = GmailApp.search(search, 0, searchSize); console.log('Found', threads.length, 'threads matching search criteria'); if (threads.length === 0) { console.log('No threads found - nothing to process'); return; } var totalProcessed = 0; var totalEmailsDeleted = 0; var batchCount = Math.ceil(threads.length / batchSize); console.log('Will process in', batchCount, 'batch(es)'); for (j = 0; j < threads.length; j += batchSize) { var currentBatch = j / batchSize + 1; var batchThreads = threads.slice(j, j + batchSize); // Count emails in this batch before deletion var emailsInBatch = 0; for (var k = 0; k < batchThreads.length; k++) { emailsInBatch += batchThreads[k].getMessageCount(); } console.log('Processing batch', currentBatch, 'of', batchCount, '- processing', batchThreads.length, 'threads containing', emailsInBatch, 'emails'); var batchStartTime = new Date(); try { GmailApp[batchAction](batchThreads); totalProcessed += batchThreads.length; totalEmailsDeleted += emailsInBatch; var batchEndTime = new Date(); var batchDuration = batchEndTime - batchStartTime; console.log('Batch', currentBatch, 'completed successfully in', batchDuration, 'ms'); console.log('Deleted', emailsInBatch, 'emails in this batch'); console.log('Total threads processed so far:', totalProcessed); console.log('Total emails deleted so far:', totalEmailsDeleted); } catch (error) { console.error('Error processing batch', currentBatch, ':', error.toString()); console.log('Continuing with next batch...'); } } var endTime = new Date(); var totalDuration = endTime - startTime; console.log('processEmail function completed'); console.log('Total execution time:', totalDuration, 'ms'); console.log('Total threads processed:', totalProcessed, 'out of', threads.length, 'found'); console.log('Total emails deleted:', totalEmailsDeleted); if (totalProcessed < threads.length) { console.warn('Warning: Not all threads were processed due to errors'); } } -
gene1wood revised this gist
Feb 1, 2023 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -32,7 +32,6 @@ Edit the file to add an oauthScopes entry for mail.google.com. It should look s { "oauthScopes": [ "https://mail.google.com/", "https://www.googleapis.com/auth/gmail.readonly", "https://www.googleapis.com/auth/gmail.modify" ], -
gene1wood revised this gist
Jan 25, 2023 . 1 changed file with 6 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -17,6 +17,8 @@ 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' @@ -29,7 +31,10 @@ Edit the file to add an oauthScopes entry for mail.google.com. It should look s { "oauthScopes": [ "https://mail.google.com/", "https://www.googleapis.com/auth/gmail.metadata", "https://www.googleapis.com/auth/gmail.readonly", "https://www.googleapis.com/auth/gmail.modify" ], "timeZone": "America/New_York", "dependencies": { -
gene1wood revised this gist
Jan 25, 2023 . 1 changed file with 31 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -15,7 +15,37 @@ 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. 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/" ], "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 -
gene1wood revised this gist
Aug 14, 2022 . 1 changed file with 37 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,31 +1,57 @@ /* 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:[email protected]', 'moveThreadsToTrash'); } function markReadLabelWork() { processEmail('label:work', 'markThreadsRead'); } function markReadFromInfoExample() { processEmail('from:[email protected]', '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)); } } -
gene1wood revised this gist
Aug 8, 2021 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -14,6 +14,9 @@ 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 */ -
gene1wood revised this gist
Aug 8, 2021 . 1 changed file with 6 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,15 +1,19 @@ /* 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). Source : # https://productforums.google.com/d/msg/gmail/YeQVDuPIQzA/kpZPDDj8TXkJ */ -
gene1wood revised this gist
Aug 8, 2021 . 1 changed file with 5 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ /* This script, when used with Google Apps Scripts will delete 400 emails and can be triggered to run every minute without user interaction enabling you to bulk delete email in Gmail without getting the #793 error from Gmail. @@ -15,8 +15,10 @@ 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:[email protected]', 0, searchSize); for (j = 0; j < threads.length; j+=batchSize) { GmailApp.moveThreadsToTrash(threads.slice(j, j+batchSize)); } } -
gene1wood revised this gist
Jul 17, 2019 . 1 changed file with 13 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,15 +1,17 @@ /* This script, when used with Google Apps Scripts will delete 500 emails and can be triggered to run every minute without user interaction enabling you to bulk delete email in Gmail without getting the #793 error from Gmail. 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). Source : # https://productforums.google.com/d/msg/gmail/YeQVDuPIQzA/kpZPDDj8TXkJ */ function batchDeleteEmail() { var batchSize = 100 // Process up to 100 threads at once -
gene1wood revised this gist
Apr 29, 2019 . 1 changed file with 8 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,14 @@ # This script, when used with Google Apps Scripts will delete 500 emails and # can be triggered to run every minute without user interaction enabling you # to bulk delete email in Gmail without getting the #793 error from Gmail. # 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). # Source : # https://productforums.google.com/d/msg/gmail/YeQVDuPIQzA/kpZPDDj8TXkJ function batchDeleteEmail() { -
gene1wood created this gist
Dec 26, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ # This script, when used with Google Apps Scripts will delete 500 emails and can be triggered to run every minute without user interaction enabling you to bulk delete email in Gmail without getting the #793 error from Gmail. # 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). # Source : # https://productforums.google.com/d/msg/gmail/YeQVDuPIQzA/kpZPDDj8TXkJ function batchDeleteEmail() { var batchSize = 100 // Process up to 100 threads at once var threads = GmailApp.search('label:inbox from:[email protected]'); for (j = 0; j < threads.length; j+=batchSize) { GmailApp.moveThreadsToTrash(threads.slice(j, j+batchSize)); } }