2

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.

3

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 | |
1

Go to https://script.google.com

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

function removeMyTest2() {

var mymail = "email@gmail.com"; // Edit this line

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.

Then 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.


Editors note:

From https://developers.google.com/apps-script/guides/services/advanced#requirements

Requirements

Before you can use an advanced service, you must satisfy the following requirements:

  • You must enable the advanced service in your script project.
  • You must make sure the API corresponding to the advanced service is enabled in the Cloud Platform (GCP) project your script uses.

If your script project uses a default GCP project created on or after April 8, 2019, the API is enabled automatically after you enable the advanced service and save the script project. If you have not done so already, you may also be asked to agree to the Google Cloud Platform and Google APIs Terms of Service as well.

If your script project uses a standard GCP project or an older default GCP project, you must enable the advanced service's corresponding API in the GCP project manually. You must have edit access to the GCP project to make this change.

See Cloud Platform projects for more information.

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

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.