Skip to content

Instantly share code, notes, and snippets.

@simon57nz
Forked from soxofaan/README.md
Last active September 11, 2018 01:47
Show Gist options
  • Save simon57nz/054f8ec4ffdccaebf121c92a1ba0fb51 to your computer and use it in GitHub Desktop.
Save simon57nz/054f8ec4ffdccaebf121c92a1ba0fb51 to your computer and use it in GitHub Desktop.
Script for autodeletion of Gmail threads based on a label structure.

Google Apps Script to automatically delete mails with a certain label after a certain time

Forked from : https://gist.github.com/soxofaan/92fab6776c1bfcac060544ba0c9dd59c

Usage

  1. Think of a deletion scheme and create GMail labels accordingly I use a three part label, parent label is "autodelete", next level is number of days, third level is descriptive. (e.g. I have labels like "autodelete/7/facebook" and "autodelete/15/linkedin")
  2. set up filters in GMail to flag desired mails with these labels
  3. create a Google Apps Script with this script (adapt function names, labels and day offsets appropriatedly) and set up triggers as desired
  4. I also star messages to prevent autodeletion.

Disclaimer

Make sure you know what your are doing and use at your own risk. I am not responsible for erroneous deletion of your emails.

function gmailAutoDelete() {
var maxDate = new Date();
var delayDays = 5;
var currLabel_parts = ["label","0","sublabel"];
// var Curr_Date = new Date();
// var Datecheck = Curr_Date.getDate();
// Get all the threads that are labelled
var labels = GmailApp.getUserLabels();
for (var a = 0; a < labels.length; a++) {
var currLabel = labels[a].getName();
currLabel_parts = currLabel.split("/");
// Parent label "for short life emails is "autodelete"
if (currLabel_parts[0] == "autodelete") {
// Second level label after autodelete is number of days to retain email
delayDays = +currLabel_parts[1];
if (delayDays > 0) {
_gmailAutoDelete(currLabel, delayDays);
}
}
}
}
// Delete Threads with given label, older than given number of days
//
// Based on:
// https://medium.com/@fw3d/auto-archive-emails-in-gmail-after-2-days-1ebf0e076b1c
// https://gist.github.com/anonymous/2cca33d376f7f924fdaa67891ad098cc
// https://gist.github.com/GabeBenjamin/3ef20889fa37ae97e9492e58e90db892
// https://gist.github.com/soxofaan/92fab6776c1bfcac060544ba0c9dd59c
function _gmailAutoDelete(labelName, minimumAgeInDays) {
Logger.log('Running autodelete for label %s (minimum age in days: %s)', labelName, minimumAgeInDays);
// Threshold for latest message of the thread.
var thresholdDate = new Date();
thresholdDate.setDate(thresholdDate.getDate() - minimumAgeInDays);
Logger.log('Using threshold date %s', thresholdDate);
// Get all the threads with the label.
var label = GmailApp.getUserLabelByName(labelName);
Logger.log('Found label %s', label);
if (label) {
Logger.log('Found label: %s', label.getName());
var batchSize = 100;
var start = 0;
while (true) {
Logger.log('Batch %s %s', start, batchSize);
var threads = label.getThreads(start, batchSize);
if (threads.length < 1) {
Logger.log('No more threads');
break;
}
var toDelete = threads.filter(function(thread) {
return (thread.getLastMessageDate() < thresholdDate && !(thread.hasStarredMessages() );
// Starred messages are not deleted
})
Logger.log('Found %s threads to delete', toDelete.length);
GmailApp.moveThreadsToTrash(toDelete)
// Prepare for next batch
start += batchSize - toDelete.length;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment