Skip to content

Instantly share code, notes, and snippets.

@mickume
Forked from jamesramsay/README.md
Created November 16, 2022 08:14
Show Gist options
  • Save mickume/9d3c83d56cc127709e3f5b51f3b4dd0b to your computer and use it in GitHub Desktop.
Save mickume/9d3c83d56cc127709e3f5b51f3b4dd0b to your computer and use it in GitHub Desktop.

Revisions

  1. James Ramsay revised this gist Jul 31, 2017. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,15 @@ If you ever want to remove the script, run `Uninstall` to remove any left over t

    ## Changelog

    ### 2017-07-31

    - Added support for multiple labels
    - Added configurable `TRIGGER_NAME`
    - Increased default page size
    - Decreased default delay between receipt and delete

    ### 2016-01-21

    - Removed use of deprecated `Session.getTimeZone()`
    - Improved efficiency for long threads by checking `thread.getLastMessageDate()`

  2. James Ramsay revised this gist Jul 31, 2017. 1 changed file with 14 additions and 8 deletions.
    22 changes: 14 additions & 8 deletions delete-by-label.js
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,19 @@
    // The name of the Gmail Label that is to be checked for purging?
    var LABEL_TO_DELETE = "direct-dev";
    var LABELS_TO_DELETE = [
    "notifications-github",
    "notifications-trello",
    "notifications-hipchat"
    ];

    var TRIGGER_NAME = "dailyDeleteGmail";

    // Purge messages in the above label automatically after how many days?
    var DELETE_AFTER_DAYS = "30";
    var DELETE_AFTER_DAYS = "4";

    var TIMEZONE = "AEST";

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

    // If number of threads exceeds page size, resume job after X mins (max execution time is 6 mins)
    var RESUME_FREQUENCY = 10;
    @@ -24,13 +30,13 @@ function Intialize() {
    function Install() {

    // First run 2 mins after install
    ScriptApp.newTrigger("deleteGmail")
    ScriptApp.newTrigger(TRIGGER_NAME)
    .timeBased()
    .at(new Date((new Date()).getTime() + 1000*60*2))
    .create();

    // Run daily there after
    ScriptApp.newTrigger("deleteGmail")
    ScriptApp.newTrigger(TRIGGER_NAME)
    .timeBased().everyDays(1).create();

    }
    @@ -44,13 +50,13 @@ function Uninstall() {

    }

    function deleteGmail() {
    function dailyDeleteGmail() {

    var age = new Date();
    age.setDate(age.getDate() - DELETE_AFTER_DAYS);

    var purge = Utilities.formatDate(age, TIMEZONE, "yyyy-MM-dd");
    var search = "label:" + LABEL_TO_DELETE + " before:" + purge;
    var search = "(label:" + LABELS_TO_DELETE.join(" OR label:") + ") before:" + purge;
    Logger.log("PURGE: " + purge);
    Logger.log("SEARCH: " + search);

    @@ -61,7 +67,7 @@ function deleteGmail() {
    // Resume again in 10 minutes
    if (threads.length == PAGE_SIZE) {
    Logger.log("Scheduling follow up job...");
    ScriptApp.newTrigger("deleteGmail")
    ScriptApp.newTrigger(TRIGGER_NAME)
    .timeBased()
    .at(new Date((new Date()).getTime() + 1000*60*RESUME_FREQUENCY))
    .create();
  3. James Ramsay revised this gist Jan 21, 2016. 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
    @@ -22,4 +22,4 @@ If you ever want to remove the script, run `Uninstall` to remove any left over t

    ## Acknowledgements

    Script orginally based on Arun's post [How to Auto Delete Old Emails In Any Gmail Label](http://www.skipser.com/p/2/p/auto-delete-email-in-any-gmail-label-.html)
    H/T: Arun's post [How to Auto Delete Old Emails In Any Gmail Label](http://www.skipser.com/p/2/p/auto-delete-email-in-any-gmail-label-.html)
  4. James Ramsay revised this gist Jan 21, 2016. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -14,3 +14,12 @@ Automatically deletes old emails that match the specified label.
    - `Install`

    If you ever want to remove the script, run `Uninstall` to remove any left over triggers.

    ## Changelog

    - Removed use of deprecated `Session.getTimeZone()`
    - Improved efficiency for long threads by checking `thread.getLastMessageDate()`

    ## Acknowledgements

    Script orginally based on Arun's post [How to Auto Delete Old Emails In Any Gmail Label](http://www.skipser.com/p/2/p/auto-delete-email-in-any-gmail-label-.html)
  5. James Ramsay revised this gist Jan 21, 2016. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -5,4 +5,12 @@ Automatically deletes old emails that match the specified label.
    ## Get started

    - Create a new Google Apps Script at [https://script.google.com](https://script.google.com)
    - Replace the empty script with the javascript below
    - Overwrite the placeholder with the javascript below
    - Update the following constants:
    - `LABEL_TO_DELETE`: the label that should be have old messages deleted
    - `DELETE_AFTER_DAYS`: the age of messsages after which they will be moved to trash
    - Save the script, then run:
    - `Initialize`
    - `Install`

    If you ever want to remove the script, run `Uninstall` to remove any left over triggers.
  6. James Ramsay created this gist Jan 21, 2016.
    8 changes: 8 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    # Gmail: delete old emails automatically

    Automatically deletes old emails that match the specified label.

    ## Get started

    - Create a new Google Apps Script at [https://script.google.com](https://script.google.com)
    - Replace the empty script with the javascript below
    90 changes: 90 additions & 0 deletions delete-by-label.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    // The name of the Gmail Label that is to be checked for purging?
    var LABEL_TO_DELETE = "direct-dev";

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

    var TIMEZONE = "AEST";

    // Maximum number of threads to process per run
    var PAGE_SIZE = 100;

    // If number of threads exceeds page size, resume job after X mins (max execution time is 6 mins)
    var RESUME_FREQUENCY = 10;

    /*
    IMPLEMENTATION
    */
    function Intialize() {
    return;
    }

    function Install() {

    // First run 2 mins after install
    ScriptApp.newTrigger("deleteGmail")
    .timeBased()
    .at(new Date((new Date()).getTime() + 1000*60*2))
    .create();

    // Run daily there after
    ScriptApp.newTrigger("deleteGmail")
    .timeBased().everyDays(1).create();

    }

    function Uninstall() {

    var triggers = ScriptApp.getProjectTriggers();
    for (var i=0; i<triggers.length; i++) {
    ScriptApp.deleteTrigger(triggers[i]);
    }

    }

    function deleteGmail() {

    var age = new Date();
    age.setDate(age.getDate() - DELETE_AFTER_DAYS);

    var purge = Utilities.formatDate(age, TIMEZONE, "yyyy-MM-dd");
    var search = "label:" + LABEL_TO_DELETE + " before:" + purge;
    Logger.log("PURGE: " + purge);
    Logger.log("SEARCH: " + search);

    try {

    var threads = GmailApp.search(search, 0, PAGE_SIZE);

    // Resume again in 10 minutes
    if (threads.length == PAGE_SIZE) {
    Logger.log("Scheduling follow up job...");
    ScriptApp.newTrigger("deleteGmail")
    .timeBased()
    .at(new Date((new Date()).getTime() + 1000*60*RESUME_FREQUENCY))
    .create();
    }

    // Move threads/messages which meet age criteria to trash
    Logger.log("Processing " + threads.length + " threads...");
    for (var i=0; i<threads.length; i++) {
    var thread = threads[i];

    if (thread.getLastMessageDate() < age) {
    thread.moveToTrash();
    } else {
    var messages = GmailApp.getMessagesForThread(threads[i]);
    for (var j=0; j<messages.length; j++) {
    var email = messages[j];
    if (email.getDate() < age) {
    email.moveToTrash();
    }
    }
    }
    }

    } catch (e) {}

    }