1

I want to autodelete any email that is classified by Gmail as spam and contains text "Bitcoin".

But trying to make filter like is:spam Bitcoin get me a message:

Filter searches containing "label:", "in:", "is:", date range (e.g. "before:" or "after:") or stars criteria (e.g. "has:yellow-star") are not recommended as they will never match incoming emails. Do you still wish to continue to the next step?

Is there some other way to achieve this or is it impossible to filter incoming email this way?

To avoid XY problem: I need to look through received spam as sometimes Gmail misclassifies it. But some words popular in spam never appear in my correspondence so something both classified as spam and having some keywords may be safely deleted without review.

1 Answer 1

1

It is possible to do this using Google Apps Script.

  1. Go to Google Apps Script, and make sure you are logged into the Google account associated with the Gmail account in question
  2. Create a new project
  3. Copy and paste the code below into the project, click on the project title and change the name to something like "Bitcoin begone", then save
  4. Create a time based trigger to run the script on a schedule (every 1 minute, 1 hour, etc.)

You can replace var searchText = "bitcoin"; with var searchText = "subject:bitcoin"; to only delete messages with bitcoin in the subject, rather than anywhere in the email.

function deleteSpam() {
  // "me" runs the script under the currently logged in Google account
  var userId = "me";
  var searchText = "bitcoin";
  var threads = GmailApp.search("in:spam " + searchText);
  for (var i = 0; i < threads.length; i++) {
    Gmail.Users.Messages.remove(userId, threads[i].getId());
  }
}

BE EXTREMELY CAREFUL!! It's possible to accidentally delete the wrong emails if you make a mistake in the script. You can test the script before creating a trigger by changing the searchText value to something else and dragging a sacrificial email into the spam folder, save the changes, then click Run on the project and see what happens. It's also not a bad idea to test scripts on a separate Gmail account first if you're not 100% comfortable with this type of scripting.

If there are certain senders or terms that would indicate that a message is not spam, you can create a regular Gmail filter to prevent those emails from going to spam in the first place. Create a filter matching emails that will never be spam and check the "Never send to spam" checkbox in the filter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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