Skip to content

Instantly share code, notes, and snippets.

@johannrichard
Last active Apr 18, 2021
Embed
What would you like to do?
E-Mail Retention Policies for Gmail

How to bring E-Mail retention policies to Gmail / Google Mail:

  • Go to Google Scripts and create a blank project (make sure you are logged into your Google account);
  • paste the code (below) (and modify the policies as needed);
  • set a trigger (Resources -> Current Project's Triggers -> Add one now) to run it at the preferred interval; and
  • create filters in Gmail/Google Mail that set policies when specific messages arrive

Inspired and adapted from Gmail Automation: 5 Useful Google Scripts to Automate Your Gmail

/****
* This Script will apply different e-mail policies according to a defined set of labels
* Actions:
* - Delete: will delete (move) the message threa to the trash (unless starred)
* - Archive: will move the thread to the archive
*
* New policies: To add a new policy, add a new entry in the "policies" map
*
* When executing the action, the corresponding label will be removed. This will speed up the
* processing of messages considerably (otherwise, the messages would be reprocessed on every run).
* Starred messages will not be deleted.
*
* By setting a timed trigger, you can execute the script in regular intervals (e.g. every 30 minutes)
* Create a filter that will apply a specific label automatically to the filtered mail and
* your mailbox will unclutter itself automatically.
*
* Inspired/adapted from https://www.maketecheasier.com/google-scripts-to-automate-gmail/
*/
function applyEmailPolicy() {
var policies = {
// Archive Policies
"monthArchive": { "label": "Archive After 30 Days", days: 30, action: "archive" },
"weekArchive": { "label": "Archive After 1 Week", days: 7, action: "archive"},
"biWeeklyArchive": { "label": "Archive After 2 Weeks", days: 14, action: "archive"},
// Delete Policiess
"monthDelete": { "label": "Delete After 30 Days", days: 30, action: "delete" },
"yearDelete": { "label": "Delete After 1 Year", days: 365, action: "delete"},
"decadeDelete": { "label": "Delete After 10 Years", days: 3650, action: "delete"},
}
for (var policyKey in policies) {
var policy = policies[policyKey];
Logger.log("Applying E-Mail Policy '" + policy["label"] + "'");
var label = GmailApp.getUserLabelByName(policy["label"]);
if(label == null){
GmailApp.createLabel(policy["label"]);
} else {
var delayDays = policy["days"];
var maxDate = new Date();
maxDate.setDate(maxDate.getDate()-delayDays);
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
if (threads[i].getLastMessageDate()<maxDate){
switch(policy["action"]) {
case "delete":
if(!threads[i].hasStarredMessages()) {
Logger.log("Deleting Thread '" + threads[i].getFirstMessageSubject() + "'");
threads[i].removeLabel(label);
threads[i].moveToTrash();
} else {
Logger.log("Skipping Thread '" + threads[i].getFirstMessageSubject() + "'");
}
break;
case "archive":
Logger.log("Archiving Thread '" + threads[i].getFirstMessageSubject() + "'");
threads[i].removeLabel(label);
threads[i].moveToArchive();
break;
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment