Gmail – automatically delete emails in Gmail trash matching specific search

gmailgmail-filters

I would like to automatically (on a schedule, or realtime) delete any emails in Gmail's trash folder that fit a specific search (NOT all Trash emails).

Can that be done? I would strongly prefer a solution not involving a 3rd party service such as IFTT; but if needed, could live with Windows program (could be a script in Python or Perl if need be) or Android app. Gmail native solutions would be best.

Best Answer

Whilst you can't do want you want with Gmail alone, you should be able to use Google Apps Script to do what you want.

Using Apps Script, you can write scripts which run predefined searches against your Gmail account and take action on the results. These can be run periodically using time-based triggers. For example, one of the ways I use this functionality is to automatically mark archived messages as read.

Unfortunately according to the documentation, the standard Gmail service doesn't have the ability to permanently delete messages, only to move them to the trash. However, there is the option to use the Advanced Gmail Service instead which provides the full power of the Gmail API, and does have a delete method.

Couple of things to note:

  1. Apps Scripts are stored in Google Drive, and must be authorised to access your Gmail account
  2. There are limits on how long any script is allowed to run, how frequently it hits the service APIs, and on how much time is allowed to be spent on a daily basis. Long-running scripts will be aborted and a warning will be sent.
  3. Triggers are specified to run within (selectable) blocks of time and will run at some point within that block - for example, you can't specify that a script is run at 02:15, only that it runs at some time between 02:00 and 03:00