Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save mfrancois3k/7a32ebf70ac66b62ff7cce78f714e1a8 to your computer and use it in GitHub Desktop.

Select an option

Save mfrancois3k/7a32ebf70ac66b62ff7cce78f714e1a8 to your computer and use it in GitHub Desktop.

Revisions

  1. mfrancois3k renamed this gist Sep 8, 2022. 1 changed file with 0 additions and 0 deletions.
  2. @gene1wood gene1wood revised this gist Aug 14, 2022. 1 changed file with 37 additions and 11 deletions.
    48 changes: 37 additions & 11 deletions batch-delete-gmail-emails.js
    Original 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
    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
    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
    Browser to https://script.google.com/.
    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 past it in, save it and click the little clock looking button.
    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() {
    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));
    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));
    }
    }
  3. @gene1wood gene1wood revised this gist Aug 8, 2021. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions batch-delete-gmail-emails.js
    Original 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
    */

  4. @gene1wood gene1wood revised this gist Aug 8, 2021. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions batch-delete-gmail-emails.js
    Original 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 minute without user interaction enabling you
    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).
    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
    */

  5. @gene1wood gene1wood revised this gist Aug 8, 2021. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions batch-delete-gmail-emails.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /*
    This script, when used with Google Apps Scripts will delete 500 emails and
    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 threads = GmailApp.search('label:inbox from:[email protected]');
    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));
    }
    }
    }
  6. @gene1wood gene1wood revised this gist Jul 17, 2019. 1 changed file with 13 additions and 11 deletions.
    24 changes: 13 additions & 11 deletions batch-delete-gmail-emails.js
    Original 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.
    /*
    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
    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
  7. @gene1wood gene1wood revised this gist Apr 29, 2019. 1 changed file with 8 additions and 3 deletions.
    11 changes: 8 additions & 3 deletions batch-delete-gmail-emails.js
    Original 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.
    # 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
    # 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).
    # 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() {
  8. @gene1wood gene1wood created this gist Dec 26, 2014.
    15 changes: 15 additions & 0 deletions batch-delete-gmail-emails.js
    Original 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));
    }
    }