Skip to content

Instantly share code, notes, and snippets.

@Adeleye080
Forked from benbjurstrom/Code.gs
Created September 16, 2022 14:03
Show Gist options
  • Select an option

  • Save Adeleye080/6bf232a9c77d201467d1836c84dc73de to your computer and use it in GitHub Desktop.

Select an option

Save Adeleye080/6bf232a9c77d201467d1836c84dc73de to your computer and use it in GitHub Desktop.

Revisions

  1. @benbjurstrom benbjurstrom revised this gist Apr 30, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # PurgeOldEmails

    A [Google Apps Script](https://developers.google.com/apps-script/) script that deletes all gmail messages from the inbox that are more than 7 days old and not starred or marked as important.
    A [Google Apps Script](https://developers.google.com/apps-script/) script to automatically delete unarchived mail after 7 days that hasn't been starred or marked as important

    ## Quick Start

  2. @benbjurstrom benbjurstrom revised this gist Apr 30, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ A [Google Apps Script](https://developers.google.com/apps-script/) script that d
    2. Load this script into a new Google Apps Script [project](https://script.google.com/).
    3. Execute the _setPurgeTrigger()_ function to set a trigger that will call the _purge()_ function every day.

    A detailed blog post with more information can be found at [https://benbjurstrom.com/purge-old-emails](https://benbjurstrom.com/purge-old-emails)
    A detailed blog post with more information can be found at [https://benbjurstrom.com/purge-email](https://benbjurstrom.com/purge-email)

    ## Acknowledgements

  3. @benbjurstrom benbjurstrom revised this gist Apr 27, 2020. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions Code.gs
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,8 @@ var DELETE_AFTER_DAYS = 7
    var PAGE_SIZE = 150

    /**
    * Create a trigger that executes the purge function every day
    * Create a trigger that executes the purge function every day.
    * Execute this function to install the script.
    */
    function setPurgeTrigger() {
    ScriptApp
    @@ -34,7 +35,7 @@ function setPurgeMoreTrigger(){
    }

    /**
    * Deletes all triggers that call the purgeMore function
    * Deletes all triggers that call the purgeMore function.
    */
    function removePurgeMoreTriggers(){
    var triggers = ScriptApp.getProjectTriggers()
    @@ -48,6 +49,7 @@ function removePurgeMoreTriggers(){

    /**
    * Deletes all of the project's triggers
    * Execute this function to unintstall the script.
    */
    function removeAllTriggers() {
    var triggers = ScriptApp.getProjectTriggers()
  4. @benbjurstrom benbjurstrom revised this gist Apr 27, 2020. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions Code.gs
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,11 @@
    /*
    |--------------------------------------------------------------------------
    | PurgeOldEmails
    |--------------------------------------------------------------------------
    | https://gist.github.com/benbjurstrom/00cdfdb24e39c59c124e812d5effa39a
    |
    */

    // Purge messages automatically after how many days?
    var DELETE_AFTER_DAYS = 7

  5. @benbjurstrom benbjurstrom revised this gist Dec 14, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Code.gs
    Original file line number Diff line number Diff line change
    @@ -67,7 +67,7 @@ function purge() {
    var threads = GmailApp.search(search, 0, PAGE_SIZE)

    if (threads.length === PAGE_SIZE) {
    console.log('Scheduling follow up job...')
    console.log('PAGE_SIZE exceeded. Setting a trigger to call the purgeMore function in 2 minutes.')
    setPurgeMoreTrigger()
    }

  6. @benbjurstrom benbjurstrom revised this gist Dec 14, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ A [Google Apps Script](https://developers.google.com/apps-script/) script that d

    1. Backup your emails using [Google Takeout](https://takeout.google.com/).
    2. Load this script into a new Google Apps Script [project](https://script.google.com/).
    3. Execute the _setTrigger()_ function to set a trigger that will call the _purge()_ function every day.
    3. Execute the _setPurgeTrigger()_ function to set a trigger that will call the _purge()_ function every day.

    A detailed blog post with more information can be found at [https://benbjurstrom.com/purge-old-emails](https://benbjurstrom.com/purge-old-emails)

  7. @benbjurstrom benbjurstrom revised this gist Dec 14, 2019. 1 changed file with 48 additions and 44 deletions.
    92 changes: 48 additions & 44 deletions Code.gs
    Original file line number Diff line number Diff line change
    @@ -1,32 +1,55 @@
    // Purge messages automatically after how many days?
    var DELETE_AFTER_DAYS = 7

    // Maximum number of message threads to process per run
    // Maximum number of message threads to process per run.
    var PAGE_SIZE = 150

    /**
    * Create a trigger that executes the purge function every day
    */
    function setTrigger() {
    function setPurgeTrigger() {
    ScriptApp
    .newTrigger('purge')
    .timeBased()
    .everyDays(1)
    .create()
    }

    /**
    * Create a trigger that executes the purgeMore function two minutes from now
    */
    function setPurgeMoreTrigger(){
    ScriptApp.newTrigger('purgeMore')
    .timeBased()
    .at(new Date((new Date()).getTime() + 1000 * 60 * 2))
    .create()
    }

    /**
    * Deletes all triggers that call the purgeMore function
    */
    function removePurgeMoreTriggers(){
    var triggers = ScriptApp.getProjectTriggers()
    for (var i = 0; i < triggers.length; i++) {
    var trigger = triggers[i]
    if(trigger.getHandlerFunction() === 'purgeMore'){
    ScriptApp.deleteTrigger(trigger)
    }
    }
    }

    /**
    * Deletes all of the project's triggers
    */
    function removeTriggers() {
    function removeAllTriggers() {
    var triggers = ScriptApp.getProjectTriggers()
    for (var i = 0; i < triggers.length; i++) {
    ScriptApp.deleteTrigger(triggers[i])
    }
    }

    /**
    * Wrapper for our purge function
    * Wrapper for the purge function
    */
    function purgeMore() {
    purge()
    @@ -38,47 +61,28 @@ function purgeMore() {
    * and not starred or marked as important.
    */
    function purge() {

    try {

    // Remove any existing triggers that call purgeMore to avoid trigger limit
    var triggers = ScriptApp.getProjectTriggers()
    for (var i = 0; i < triggers.length; i++) {
    var trigger = triggers[i]
    if(trigger.getHandlerFunction() === 'purgeMore'){
    ScriptApp.deleteTrigger(trigger)
    }
    }

    var cutoff = new Date()
    cutoff.setDate(cutoff.getDate() - DELETE_AFTER_DAYS)

    var search = 'in:inbox -in:starred -in:important older_than:' + DELETE_AFTER_DAYS + 'd'
    var threads = GmailApp.search(search, 0, PAGE_SIZE)

    // If the number of threads is equal to our page size
    if (threads.length === PAGE_SIZE) {
    console.log('Scheduling follow up job...')

    // Set a trigger to call the purgeMore method in two minutes
    ScriptApp.newTrigger('purgeMore')
    .timeBased()
    .at(new Date((new Date()).getTime() + 1000 * 60 * 2))
    .create()
    }

    console.log('Processing ' + threads.length + ' threads...')
    removePurgeMoreTriggers()

    var search = 'in:inbox -in:starred -in:important older_than:' + DELETE_AFTER_DAYS + 'd'
    var threads = GmailApp.search(search, 0, PAGE_SIZE)

    if (threads.length === PAGE_SIZE) {
    console.log('Scheduling follow up job...')
    setPurgeMoreTrigger()
    }

    console.log('Processing ' + threads.length + ' threads...')

    var cutoff = new Date()
    cutoff.setDate(cutoff.getDate() - DELETE_AFTER_DAYS)

    // For each thread matching our search
    for (var i = 0; i < threads.length; i++) {
    var thread = threads[i]

    // For each thread matching our search
    for (var i = 0; i < threads.length; i++) {
    var thread = threads[i]

    // Only delete if the newest message in the thread is older then DELETE_AFTER_DAYS
    if (thread.getLastMessageDate() < cutoff) {
    thread.moveToTrash();
    }
    // Only delete if the newest message in the thread is older then DELETE_AFTER_DAYS
    if (thread.getLastMessageDate() < cutoff) {
    thread.moveToTrash();
    }
    } catch (e) {
    console.log(e)
    }
    }
  8. @benbjurstrom benbjurstrom revised this gist Dec 14, 2019. 1 changed file with 24 additions and 7 deletions.
    31 changes: 24 additions & 7 deletions Code.gs
    Original file line number Diff line number Diff line change
    @@ -25,26 +25,43 @@ function removeTriggers() {
    }
    }

    /**
    * Wrapper for our purge function
    */
    function purgeMore() {
    purge()
    }


    /**
    * Deletes any emails from the inbox that are more then 7 days old
    * and not starred or marked as important.
    */
    function purge() {

    var cutoff = new Date()
    cutoff.setDate(cutoff.getDate() - DELETE_AFTER_DAYS)

    var search = 'in:inbox -in:starred -in:important older_than:' + DELETE_AFTER_DAYS + 'd'

    try {

    // Remove any existing triggers that call purgeMore to avoid trigger limit
    var triggers = ScriptApp.getProjectTriggers()
    for (var i = 0; i < triggers.length; i++) {
    var trigger = triggers[i]
    if(trigger.getHandlerFunction() === 'purgeMore'){
    ScriptApp.deleteTrigger(trigger)
    }
    }

    var cutoff = new Date()
    cutoff.setDate(cutoff.getDate() - DELETE_AFTER_DAYS)

    var search = 'in:inbox -in:starred -in:important older_than:' + DELETE_AFTER_DAYS + 'd'
    var threads = GmailApp.search(search, 0, PAGE_SIZE)

    // If the number of threads is equal to our page size
    if (threads.length === PAGE_SIZE) {
    console.log('Scheduling follow up job...')

    // Re-trigger the purge method again after two minutes
    ScriptApp.newTrigger('purge')
    // Set a trigger to call the purgeMore method in two minutes
    ScriptApp.newTrigger('purgeMore')
    .timeBased()
    .at(new Date((new Date()).getTime() + 1000 * 60 * 2))
    .create()
  9. @benbjurstrom benbjurstrom revised this gist Dec 14, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    A [Google Apps Script](https://developers.google.com/apps-script/) script that deletes all gmail messages from the inbox that are more than 7 days old and not starred or marked as important.

    ## Quick Start

    1. Backup your emails using [Google Takeout](https://takeout.google.com/).
    2. Load this script into a new Google Apps Script [project](https://script.google.com/).
    3. Execute the _setTrigger()_ function to set a trigger that will call the _purge()_ function every day.
    @@ -11,4 +12,4 @@ A [Google Apps Script](https://developers.google.com/apps-script/) script that d

    ## Acknowledgements

    Thanks to [this gist](https://gist.github.com/jamesramsay/9298cf3f4ac584a3dc05) by jamesramsay
    Thanks to [this gist](https://gist.github.com/jamesramsay/9298cf3f4ac584a3dc05) by jamesramsay for getting me started in the right direction.
  10. @benbjurstrom benbjurstrom revised this gist Dec 14, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ A [Google Apps Script](https://developers.google.com/apps-script/) script that d
    ## Quick Start
    1. Backup your emails using [Google Takeout](https://takeout.google.com/).
    2. Load this script into a new Google Apps Script [project](https://script.google.com/).
    3. Execute the /setTrigger()/ function to set a trigger that will call the /purge()/ function every day.
    3. Execute the _setTrigger()_ function to set a trigger that will call the _purge()_ function every day.

    A detailed blog post with more information can be found at [https://benbjurstrom.com/purge-old-emails](https://benbjurstrom.com/purge-old-emails)

  11. @benbjurstrom benbjurstrom revised this gist Dec 14, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,13 @@
    # PurgeOldEmails

    A [Google Apps Script](https://developers.google.com/apps-script/) script that deletes all gmail messages from the inbox that are more then 7 days old and not starred or marked as important.
    A [Google Apps Script](https://developers.google.com/apps-script/) script that deletes all gmail messages from the inbox that are more than 7 days old and not starred or marked as important.

    ## Quick Start
    1. Backup your emails using [Google Takeout](https://takeout.google.com/).
    2. Load this script into a new Google Apps Script [project](https://script.google.com/).
    3. Execute the /setTrigger()/ function to set a trigger that will call the /purge()/ function every day.

    A detailed blog post with more infomration can be foud at [https://benbjurstrom.com/purge-old-emails](https://benbjurstrom.com/purge-old-emails)
    A detailed blog post with more information can be found at [https://benbjurstrom.com/purge-old-emails](https://benbjurstrom.com/purge-old-emails)

    ## Acknowledgements

  12. @benbjurstrom benbjurstrom revised this gist Dec 14, 2019. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,13 @@
    # PurgeOldEmails

    This is a [Google Apps Script](https://script.google.com/) intended to delete all emails from the inbox that are more then 7 days old and not starred or marked as important.
    A [Google Apps Script](https://developers.google.com/apps-script/) script that deletes all gmail messages from the inbox that are more then 7 days old and not starred or marked as important.

    ## Quick Start
    1. Backup your emails using [Google Takeout](https://takeout.google.com/).
    2. Load this script into a new Google Apps Script [project](https://script.google.com/).
    3. Execute the /setTrigger()/ function to set a trigger that will call the /purge()/ function every day.

    A detailed blog post with more infomration can be foud at [https://benbjurstrom.com/purge-old-emails](https://benbjurstrom.com/purge-old-emails)

    ## Acknowledgements

  13. @benbjurstrom benbjurstrom revised this gist Dec 13, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Code.gs
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@ function purge() {
    var cutoff = new Date()
    cutoff.setDate(cutoff.getDate() - DELETE_AFTER_DAYS)

    var search = 'in:inbox -in:starred -in:important older_than:7d'
    var search = 'in:inbox -in:starred -in:important older_than:' + DELETE_AFTER_DAYS + 'd'

    try {
    var threads = GmailApp.search(search, 0, PAGE_SIZE)
  14. @benbjurstrom benbjurstrom revised this gist Dec 13, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Code.gs
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // Purge messages in the above label automatically after how many days?
    // Purge messages automatically after how many days?
    var DELETE_AFTER_DAYS = 7

    // Maximum number of message threads to process per run
  15. @benbjurstrom benbjurstrom revised this gist Dec 13, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions Code.gs
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    // Maximum number of message threads to process per run
    var PAGE_SIZE = 150

    // Purge messages in the above label automatically after how many days?
    var DELETE_AFTER_DAYS = 7

    // Maximum number of message threads to process per run
    var PAGE_SIZE = 150

    /**
    * Create a trigger that executes the purge function every day
    */
  16. @benbjurstrom benbjurstrom revised this gist Dec 13, 2019. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    # PurgeOldEmails

    This is a [Google Apps Script](https://script.google.com/) intended to delete all emails from the inbox that are more then 7 days old and not starred or marked as important.
    This is a [Google Apps Script](https://script.google.com/) intended to delete all emails from the inbox that are more then 7 days old and not starred or marked as important.

    ## Acknowledgements

    Thanks to [this gist](https://gist.github.com/jamesramsay/9298cf3f4ac584a3dc05) by jamesramsay
  17. @benbjurstrom benbjurstrom revised this gist Dec 13, 2019. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    # PurgeOldEmails

    This is a google scripts application intended to delete all emails from the inbox that are more then 7 days old
    and not starred or marked as important.
    This is a [Google Apps Script](https://script.google.com/) intended to delete all emails from the inbox that are more then 7 days old and not starred or marked as important.
  18. @benbjurstrom benbjurstrom revised this gist Dec 13, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #PurgeOldEmails
    # PurgeOldEmails

    This is a google scripts application intended to delete all emails from the inbox that are more then 7 days old
    and not starred or marked as important.
  19. @benbjurstrom benbjurstrom revised this gist Dec 13, 2019. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    #PurgeOldEmails

    This is a google scripts application intended to delete all emails from the inbox that are more then 7 days old
    and not starred or marked as important.
  20. @benbjurstrom benbjurstrom created this gist Dec 13, 2019.
    67 changes: 67 additions & 0 deletions Code.gs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    // Maximum number of message threads to process per run
    var PAGE_SIZE = 150

    // Purge messages in the above label automatically after how many days?
    var DELETE_AFTER_DAYS = 7

    /**
    * Create a trigger that executes the purge function every day
    */
    function setTrigger() {
    ScriptApp
    .newTrigger('purge')
    .timeBased()
    .everyDays(1)
    .create()
    }

    /**
    * Deletes all of the project's triggers
    */
    function removeTriggers() {
    var triggers = ScriptApp.getProjectTriggers()
    for (var i = 0; i < triggers.length; i++) {
    ScriptApp.deleteTrigger(triggers[i])
    }
    }

    /**
    * Deletes any emails from the inbox that are more then 7 days old
    * and not starred or marked as important.
    */
    function purge() {

    var cutoff = new Date()
    cutoff.setDate(cutoff.getDate() - DELETE_AFTER_DAYS)

    var search = 'in:inbox -in:starred -in:important older_than:7d'

    try {
    var threads = GmailApp.search(search, 0, PAGE_SIZE)

    // If the number of threads is equal to our page size
    if (threads.length === PAGE_SIZE) {
    console.log('Scheduling follow up job...')

    // Re-trigger the purge method again after two minutes
    ScriptApp.newTrigger('purge')
    .timeBased()
    .at(new Date((new Date()).getTime() + 1000 * 60 * 2))
    .create()
    }

    console.log('Processing ' + threads.length + ' threads...')

    // For each thread matching our search
    for (var i = 0; i < threads.length; i++) {
    var thread = threads[i]

    // Only delete if the newest message in the thread is older then DELETE_AFTER_DAYS
    if (thread.getLastMessageDate() < cutoff) {
    thread.moveToTrash();
    }
    }
    } catch (e) {
    console.log(e)
    }
    }