In the past I have used a simple google apps script snippet to delete all spam email from my Gmail account once an hour. This solution worked however it meant I sometimes would not receive emails from people that Google chose to move to the spam folder. I decided the best solution would be to delete only the emails which contained certain keywords. After failing to find an example online of how to do this I came up with a solution after some research on the Gmail apps script API
Script source: Google Apps Script - Gmail, delete forever e-mails in trash with specific label
The original code to delete all email:
function deleteForever(userId) {
var threads = GmailApp.search("in:spam");
for (var i = 0; i < threads.length; i++) {
Gmail.Users.Messages.remove(userId, threads[i].getId().toString());
}
}
deleteForever("[your_email_here@gmail.com]")
Please note that "in:spam" can be changed to any location in your Gmail account. Be very careful when choosing the threads you wish to delete!
The solution required two pieces of information I didn't have before:
Rather than blindly deleting every message in the threads variable I use a very basic regular expression to match a list of keywords and only delete messages which match.
When using this script add a pipe | between words and wrap phrases which contain spaces in parentheses as shown below.
function deleteForevers(userId) {
var threads = GmailApp.search("in:spam");
for (var i = 0; i < threads.length; i++) {
var msg = threads[i].getMessages()[0];
if (msg.getPlainBody().match(/prizes|miracle|(real estate)|(nigerian prince)/gi) !== null){
Gmail.Users.Messages.remove(userId, threads[i].getId().toString());
}
}
}
deleteForevers("[your_email_here@gmail.com]")
I hope this is helpful!