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:
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!
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!
Comments
Post a Comment