This script will delete emails old than 30 days with a specified label.
Focussing on emails that are from a mailing list I signed up for. I used Gmails filters to move specific email to a label called "Mailing List". You can set the label to "inbox" if you'd like but this would delete everything in your inbox older than 30 days.
To protect your inbox I suggest moving emails you know are safe to delete to a new label.
- From your inbox click the checkbox next to the email > More > "Filter Messages Like this"
- Select how this filter selects the email (either From, Subject, To, etc)
- Create Filter with this search >>
- Check these 2
- Skip the Inbox
- Apply Label> New Label > Label Name: Mailing List > Create
- Create Filter
Note: If the label doesn't already exist you'll need to create one with "New Label"
Go to Google Scripts > Start Scripts
Change "Untitled Project" to anything you'd like
It should give you a default file "Code.gs" with a sample "myFunction".
Lefts replace it with this.
Code.gs
You can change "Mailing List" to any label you are using
function cleanUp() {
var delayDays = 30 // Enter # of days before messages are moved to trash
var maxDate = new Date();
maxDate.setDate(maxDate.getDate()-delayDays);
var label = GmailApp.getUserLabelByName("Mailing List");
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
if (threads[i].getLastMessageDate()<maxDate)
{
threads[i].moveToTrash();
}
}
}
- File Save
- Run > Cleanup > Authorize the script to access your Gmail account
- Resources > "Current Project's Triggers"
cleanup | Events | ||
---|---|---|---|
cleanup | Time-driven | Day timer | Midnight to 1am |
- SAVE
- File Save
- Run > Cleanup
That's it!
http://draft.sx/0d59ca89b8efdbfe1c0429c258444fe7