Skip to content

Instantly share code, notes, and snippets.

@Jun-Dai
Created March 1, 2015 21:19
Show Gist options
  • Save Jun-Dai/b2e5f2e38d2b0259d51c to your computer and use it in GitHub Desktop.
Save Jun-Dai/b2e5f2e38d2b0259d51c to your computer and use it in GitHub Desktop.
GoogleScript for purging old e-mails based on patterns
var CONFIGS = [
{ search: "label:github", after_days: 5 },
{ search: "label:tlug", after_days: 30 },
{ search: "from:Trello", after_days: 30 },
{ search: "from:Quora Digest", after_days: 10 }
];
var SAVE_EMAIL_LABEL = "saved";
var EMAIL_FOR_SUMMARIES = "you@me.com";
var SEARCH_LIMIT = 100;
/*
Inspired by: http://labnol.org/?p=27605
I N S T R U C T I O N S
- - - - - - - - - - - -
Step 1. Update the configuration at the top of this file:
- CONFIGS: an array of things to purge, each has a `search` and an `after_days` attribute
- SAVE_EMAIL_LABEL: any e-mail with this label will not be deleted
- EMAIL_FOR_SUMMARIES: If provided, the script will send an e-mail of everything purged to this address.
- SEARCH_LIMIT: Number of threads to allow searching on. I recommend 100.
This will delete up to a few hundred e-mails per config each time this script is run.
Lower this if you run into problems with rate-limits.
Step 2. Go to Run -> Initialize and authorize the script.
Step 3. Go to Run -> Install to install the script.
You can now exit this window and any email messages in Gmail folder will automatically
get purged after 'n' days. The script will run by itself everyday at 01:00 hours.
Also, you may go to Run -> Uninstall to stop the purging script anytime.
*/
function Initialize() {
return;
}
function Install() {
ScriptApp.newTrigger("runGmailPurges")
.timeBased()
.at(new Date((new Date()).getTime() + 1000*60*2))
.create();
ScriptApp.newTrigger("runGmailPurges")
.timeBased().everyDays(1).create();
}
function Uninstall() {
var triggers = ScriptApp.getScriptTriggers();
for (var i=0; i<triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
}
}
function runGmailPurges() {
for (var i=0; i<CONFIGS.length; i++) {
runGmailPurge(CONFIGS[i]);
}
}
function runGmailPurge(config) {
var before_date = HELPERS.getBeforeDate(config.after_days);
var search = HELPERS.generateSearch(config, before_date);
var threads = GmailApp.search(search, 0, SEARCH_LIMIT);
GmailApp.moveThreadsToTrash(threads);
if (EMAIL_FOR_SUMMARIES) {
HELPERS.sendEmailSummary(config, search, threads);
}
}
HELPERS = {
generateSearch: function(config, before_date) {
var label_clause = SAVE_EMAIL_LABEL ? "NOT label:" + SAVE_EMAIL_LABEL + " " : "";
return label_clause + config.search + " before:" + before_date;
},
getBeforeDate: function(daysAgo) {
var age = new Date();
age.setDate(age.getDate() - daysAgo);
return Utilities.formatDate(age, Session.getScriptTimeZone(), "yyyy-MM-dd");;
},
getFromName: function(from) {
matches = /^(.*) <[^>]+>/.exec(from);
if (matches) {
return matches[1];
} else {
return from;
}
},
sendEmailSummary: function sendEmailSummary(config, search, threads) {
var summary_msg = "Summary of deletions performed for search: " + search;
var messages = [];
for (var i=0; i<threads.length; i++) {
messages = messages.concat(GmailApp.getMessagesForThread(threads[i]));
}
for (var j=0; j<messages.length; j++) {
var email = messages[j];
summary_msg += '\t' + Utilities.formatDate(email.getDate(), "GMT", "yyyy-MM-dd") + ' - From: ' + HELPERS.getFromName(email.getFrom()) + " - " + email.getSubject() + "\n";
}
summary_msg += "Deleted " + messages.length + " messages in total from search '" + search + "'";
if(messages.length > 0) {
MailApp.sendEmail(EMAIL_FOR_SUMMARIES, "Deleted " + messages.length + " e-mails from search '" + search + "'", summary_msg);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment