Sunday 20 April 2014

Gmail - Purging promotional emails

Today I thought I would go about looking for a a way to set some sort of "Expires after x days" type rules. I don't mind getting promotional emails though I find every few months after neglecting to purge these from my inbox they amass to a level where I spend a day or two worth of "on the train" time bulk deleting them. I came across https://www.johneday.com/422/time-based-gmail-filters-with-google-apps-script which gave me what I was aiming to do using Google scripts.



What I have done is set-up a daily, weekly, fortnightly, and monthly set of purge rules for various newsletters and promotional emails I receive and bucketed the emails based on how frequently I get spammed from the various senders, and to ensure it is purged when it is no longer current or likely relevant. The nice thing about this approach is that I can always clear the deletion label and it will no longer be collected.

I created my labels and assigned a few dozen emails to the various buckets (for example: things like Google calendar reminders, Amazon Local and Living Social for example are in my daily purge and some of my monthly newsletters are in the 30 day purge rule).

Then head to https://script.google.com and setup a new "Gmail" project which gives you a bunch of template functions. I then added my own functions which I've copied in here for convenience:

/**
 * Trash all emails with "Delete/1 Day" after 1 day
 */
function deleteByLabelDelete1Day() { 
  deleteByLabel('delete/1 day', 1);
}

/**
 * Trash all emails with "Delete/7 Days" after 7 days
 */
function deleteByLabelDelete7Days() { 
  deleteByLabel('delete/7 days', 7);
}

/**
 * Trash all emails with "Delete/14 Days" after 14 days
 */
function deleteByLabelDelete14Days() { 
  deleteByLabel('delete/14 days', 14);
}

/**
 * Trash all emails with "Delete/30 Days" after 30 days
 */
function deleteByLabelDelete30Days() { 
  deleteByLabel('delete/30 days', 30);
}

/**
 * Trash all emails matching specified "label" older than "days"
 */
function deleteByLabel(label, days) { 
  var lbl = GmailApp.getUserLabelByName(label);
  var query = 'label:' + label + ' older_than:' + days + 'd';
  var threads = GmailApp.search(query);

  var batch = 100; // Size of batch per thread
  for (i = 0; i < threads.length; i+=batch) {
    GmailApp.moveThreadsToTrash(threads.slice(i, i+batch));
    lbl.removeFromThreads(threads.slice(i, i+batch));
  }
}
Once you are happy (debug tests work for example) you can select Resources -> "Current project's triggers" and specify the triggers you want. I have the following 4 triggers:


If prompted you will need to accept the app permissions request for your new script to access Gmail via the "View and manage your mail" permission.

Happy Scripting!

5 comments:

  1. You ought to be a part of a contest for one of the most effective blogs on the net. I’ll suggest this blog! hotmail sign in

    ReplyDelete
  2. A debt of gratitude is in order for the blog entry amigo! Keep them coming... Buy Old Gmail Accounts

    ReplyDelete
  3. We are truly thankful for your blog entry. You will discover a great deal of methodologies in the wake of going to your post. I was precisely scanning for. A debt of gratitude is in order for such post and please keep it up.  bsdisplays

    ReplyDelete
  4. Compulsive stock trading additionally lacks the social stigma that will act as a deterrent towards conventional forms of gambling, even though fact} that|although} the stimulation behind both is comparable, they said. Whether you are looking at enterprise sectors in 1xbet the subsequent city or crosswise over continents, we understand the significance of being acquainted with the client’s purchase. We overcome our clients’ points by recognizing and deciphering the target group and producing leads with utmost precision. We seek to collaborate with our purchasers to ship a broad spectrum of outcomes via a mix of market and enterprise analysis approaches. Many foreigners do wrestle with this, and gambling addiction in South Korea has become an enormous drawback. Another way have the ability to|you possibly can} deposit cash right into a gambling web site is by using cryptocurrency.

    ReplyDelete
  5. Thank you very much, this is very good stuff

    ReplyDelete