Google Apps Script that moves Gmail messages to TRASH based on a User Label containing a time offset which is then compared to the message's actual age. Labels can be manually applied or applied using a Gmail Filter. The script uses regex to detect existing and new labels and multiple offset labels can hapily coexist.
This tool is not case sensitive.
<Label> = <prefix>[<sepparator01>]<delay>[<sepparator01>]<unit>
<prefix>: 'delete', 'del', 'trash', 'tra' //
<delay>: '#' or '0.#' 🠖 positive rational numbers; no thousands separator;
can include decimal include leading 0 if <1 ;
Examples: '1', '100', '0.258', '125.14563'
<unit>: 'h*' 🠖 hours 🠖 Examples: 'h' or 'hrs' or 'hour' or 'habcdefg'
'd*' 🠖 days
'm*' 🠖 months
'y*' 🠖 years
[<sepparator01>]: Optional, but must be one of:
🠖 A single underscore, or non-word character (\W)
🠖 The word 'in' or 'after' optionally preceded and/or
followed by a single underscore, or non-word character
Examples: '.', '_', 'in', '.in', '_in.', 'in_'
[<sepparator02>]: Optional, a single underscore, or non-word character (\W)
- 1=enabled, 0=disabled (actually anything but 1)
- If testMode enabled, nothing actually gets moved to TRASH
- Enabling useTestLabel is optional, and always ignored if testMode is disabled
- Enabling useTestLabel searches Gmail for the testLabel only, instead of filtering all User Labels based on the Label Syntax (see "Label Syntax" above")
- Variable testLabel is ignored if useTestLabel is disabled & always if testMode is disabled
A. Create a Google Apps Script (GAS) Project
B. Paste the Code.gs file contents into your GAS Project's Code.gs file.
C. Add the dependency Advanced Gmail API Service to your GAS Project.
D. Authorize project to access your Gmail.
E. Create Trigger
- Sign in to your Google Account in Chrome web browser.
- Navigate to script.google.com If this is the first time you've been to script.google.com, click View Dashboard.
- At the top left, click 'New project'.
- Name the project by clicking on 'Untitled project'.
- Delete any pre-populated code from the script editor (e.g. function myFunction(), etc.)
- Using a text editor copy the contents the Code.gs file in this Git and paste it into the script editor.
- Click the Save button.
- Click on the plus (+) symbol on 'Services +' to open the 'Add a service' dialog.
- Type 'Gmail' in the 'Identifier' field.
- Click 'Add'
- Open Code.gs in the script editor
- Make sure 'addParentLabel' is the function selected to the right of 'Run' and 'Debug'
- Click on the 'Run' button beside 'Debug' at the top of the page.
- Click on 'Review permissions' in the 'Authorization required' dialog that appears.
- Choose the Google account you'd like to allow the script to access.
- You will see a warning that 'Google hasn’t verified this app' because... Google Hasn't Verified this App :-)
- Click on the 'Advanced' link
- Click on the 'Go to [custom script name here] (unsafe)' link.
- Read the warning about the fact that this script wants to access your Gmail account.
- Click 'Allow' if you'd like to allow this script access.
Please Note:
If you completed steps 11 - 20, but took too long, that script instance will have timed
out with a permission-related error. Don't worry, future instances will succeed. Simply
manually run the script again to confirm by repeating steps 12 - 14. You should no longer
be prompted for authorization.
- Click on the 'Trigger' menu button (alarm clock image) in the left menu.
- Click the '+ Add Trigger' button at the bottom right of the Triggers page.
- Configure Trigger:
Trigger Settings:
Choose which function to run: trashByExpiryLabel
Choose which deployment should run: Head
Select event source: Time-driven
Set the following to your preference. Here are my settings:
Select type of time based trigger: Minutes timer
Select minute interval: Every 5 minutes
Failure notification settings: Notify me immediately
Constructive feedback is welcomed and, of course, please advise of any issues/bugs encountered.