A Google Apps Script script to automatically delete unarchived mail after 7 days that hasn't been starred or marked as important
- Backup your emails using Google Takeout.
- Load this script into a new Google Apps Script project.
- Execute the setPurgeTrigger() function to set a trigger that will call the purge() function every day.
A detailed blog post with more information can be found at https://benbjurstrom.com/purge-email
Thanks to this gist by jamesramsay for getting me started in the right direction.
Ben--thanks for this!
@blackstatic42
I think every message in gmail is 'labeled' with at least a folder label, so omitting them would be effectively omitting ALL messages. You could probably skip specific labels like this
Add your label list near the top like this:
// Purge messages with these labels: var LABELS_TO_SAVE = [ "mailinglist", "Updates", "Social", "Promotions" ];
Then change line 76 (starts with 'var search') to this:
var search = "(-label:" + LABELS_TO_SAVE.join(" OR -label:") + ") in:inbox -in:starred -in:important older_than:" + DELETE_AFTER_DAYS + 'd'
You can also make this a list of labels to delete--just rename the var to "LABELS_TO_DELETE" in both places for clarity, and remove the "-" before both 'label:' entries.