This is an overhauled approach to an older script that I was running for a while to clean up my Gmail. This script can delete old emails automatically and sort old emails from the inbox to folders (labels).
- Create a new Google Apps Script at https://script.google.com and overwrite the placeholder content with the javascript below.
- Create a google spreadsheet which will be used to store and easily update your configurations.
- In the Spreadsheet create a table with the same headers from the following example.
| Labels | Sender_Addresses | filter_to |
|---|---|---|
| Shopping | noreply@mailer.com | Mailer |
| Junk | ||
| Emails From Jim |
- The 1st column is just a list of the labels (folders) that you have in your Gmail that you already filter things into with the typical gmail filters. The default setting of the script is to delete emails from these folders when they reach 120+ days old.
- The 2nd column is email addresses that you receive emails from on a regular basis that you want to move from the inbox to a folder after the specified amount of time in the script (120 days).
- The 3rd column is for the labels (folders) that you want to filter the email in the adjacent column to after the specified amount of days.
- Columns 2 and 3 are optional and it essentially works like a filter rule on your inbox that is on a delay.
- Open the script file and configure your settings.
- Grab the ID of the spreadsheet which you setup for your configuration. The ID is the part of the URL of the spreadsheet which is inbetween the
.../d/and the/edit.... Example:https://docs.google.com/spreadsheets/d/icvp_M9Sk5gg4-f_xoJWK/edit#gid=0. The ID of this URL would beicvp_M9Sk5gg4-f_xoJWK. Paste the ID into theSPREADSHEET_IDvariable in the script file. - Edit the
DELETE_TIME_THRESHOLDandFILTER_TIME_THRESHOLDvalues if you would like to set them to different numbers of days. - To the left menu of the script file select Triggers. ⏰
- Setup your trigger to execute the script's
mailManfunction on a reoccurring basis of your choosing. I like to have mine run every night at 1am. - Naviage back to the script editor via the left menu and then at the top select the
installfunction and run it. - Then select the
mailManfunction and run it to run your first batch. You will see the console output in the executions pane informing you of how many emails are being handled.
- Grab the ID of the spreadsheet which you setup for your configuration. The ID is the part of the URL of the spreadsheet which is inbetween the
- Fin.
Whats YOUR_SHEET_ID_HERE?