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.


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
| improve this answer | |

Go to https://script.google.com

Then create a new project and paste the below into it:

function removeMyTest2() {

var mymail = "email@gmail.com";

var mylabel = "trash";

var permanentlyRemoveMyLabel = true;

var pageToken;

do {

    var threadList = Gmail.Users.Threads.list('me', {

    q: 'in:' + mylabel,

    pageToken: pageToken


    if (threadList.threads && threadList.threads.length > 0) {

        threadList.threads.forEach(function(thread) {

        Logger.log('id: %s snippet: %s', thread.id, thread.snippet);

        if (permanentlyRemoveMyLabel) {

            Gmail.Users.Threads.remove(mymail, thread.id);

            Logger.log('id: %s snippet: %s REMOVED', thread.id, thread.snippet);




   pageToken = threadList.nextPageToken;

   } while (pageToken);


If you try and run the script as is it will error out.

Next you click on Resources and go to Advanced Google Services.

Scroll down to Gmail API and turn that on.

Next click Google Cloud Platform Link on that same window that came up where you enabled the Gmail API.

Then click on the Enable services and API and search for Gmail. Click on Gmail and then enable it.

Then go back to your script screen and click on the menu Edit and go to Current project's triggers.

Set a trigger to run the script either every minute/hour or whatever time interval that you want the script to run and empty your trash.

Keep in mind you can delete from IN:TRASH IN:SPAM or whatever other label you want. It won't delete reminders and the deletion is final. You can't recover any of the emails.

| improve this answer | |
  • This actually works, though a few of the steps aren't quite precise. Thanks! – Telmo Trooper Mar 19 '19 at 13:21

You can't do that with just Gmail. Really the only automation available in Gmail is filters, and they only act on messages as they come in. Further, they can only add messages/conversations to Trash; they can't permanently delete anything.

Your only option will be a third-party one, if something exists.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.