GMail Scripts


GMail isn’t an Enterprise email client like Microsoft Outlook. It has some gaps. One of those gaps is to automatically mark the messages as Read when they go into the Trash folder. Unfortunately GMail doesn’t natively mark theses messages as read. Thankfully Google has a scripts web site that can accomplish this task. Below are the steps.

Create the script

  1. Go to the site https://script.google.com.
  2. Login if you already haven’t done so.
  3. Click on the New Script button.
  4. A new window will appear to where you can enter in the text for the script.
  5. Give your project a name.
  6. Enter in the code in the text window.

function markAllTrashAsRead() {

var threads = GmailApp.search(‘in:trash label:unread’);

GmailApp.markThreadsRead(threads);

};

The code will look for threads with the label “in:trash label:unread”. Once the threads have been retrieved the thread messages will be marked as read.

Below is a sample screenshot of the editor window.


Run and Test the Script

Below are the steps to run and test the script to make sure it’s running correctly.

  1. Run the task by clicking the run button on the toolbar.
  2. Google may then ask for permission for the script to access your GMail account. Accept the permissions and the task should run.
  3. The task should run and finally mark those messages in the Trash folder as read.


Set the Timer

The timer is a handy feature to have the script automatically execute on the Google Servers at a specified time interval. No additional windows or web service is needed to execute the script. Below are the steps to set it up.

  1. Click the Timer button.
  2. Below is the screenshot that shows the timer window.
  3. You can setup any timer interval like minute, hourly, daily, weekly, etc.
  4. Click the Save button to save the timer.


Other Sample Scripts

// delete calendar invite emails older than 14 days.

function moveCalendarMsgsToTrash() {

var threads = GmailApp.search(‘filename:invite.ics older_than:14d’);

GmailApp.moveThreadsToTrash(threads);

};