Trash and auto purge from Gmail. How to leverage Apps Scripts to make your life easier!

Wait 5 sec.

\Getting unwanted emails from a specific sender and can’t resist checking the trash? This guide will help you remove them for good.\:::warningNote: This article may contain affiliate links to Amazon. You won’t be charged anything extra for clicking on these links, but you will be supporting Hackernoon if you do choose to purchase something through one of these links.:::\This post is a bit different from my usual content. While it includes a snippet of code, it's designed for a less technical audience, thus the flood of screenshots.\I received an atypical cry for help. A couple is splitting and one of the partners is spamming the other with abusive emails. The receiver created a Gmail filter to trash the messages, but Gmail trash retention policy is 30 days and, in moments of weakness, can't resist clicking in the trash and reading them, which understandably exacerbates the issue.\Given the violent nature of the emails, I didn’t want to risk deleting potential evidence, so my suggestion was to create a process that would:\Auto-forward the emails from that sender to a lawyerDelete those emails and purge them from trash, bypassing Gmail's 30-day retention policy\\Create a forward addressOn this particular case, the forward address will be the lawyer. Go to Gmail settings, select "Forwarding and POP/IMAP" and click on "Add a forwarding address".\ \Remember to keep forwarding DISABLED, we only want to forward emails from a single senderAfter adding a forward address, Gmail will send a confirmation to that email - your lawyer - asking for permission. As soon as that's granted, you may proceed to the next step. Just remember to keep Forwarding disabled!Create a new FilterHere we will be specifying that all emails that come from someone@example.com will be forwarded to laywer@example.com and then sent to the trash.\Go to "Filters and Blocked Addresses" and then click on "Create a new filter".\ The form for adding filters will open. You want all messages from that specific address to be filtered, so just add "[email protected]" to the filter and choose "Create filter".\ \Now you'll have to select exactly what you want the filter to do. On our specific case, we'll forward the email to the lawyer, so tick that box. We also want to delete the email, so also tick that one.\ \This solves half of the issue and is the most straightforward part of the process. The tricky bit comes next, how to instantly purge those emails from the trash so that we're not tempted to read them. Google Apps Script to the rescue!Create a Google Apps ScriptGoogle Drive offers a feature that allows you to host and run scripts. While most developers are familiar with this, power users may not be aware of it. For the task at hand, this feature is absolutely perfect.\Head into https://script.google.com/, follow the authentication procedures, if needed, and then click on "New Project".\ \You're now on your project screen. You'll need to interact with Gmail so let's add that service. Click on the big + next to "Services", look for Gmail API and add it.\ \Now, replace the myFunction with this piece of code. Remember, YOU MUST CHANGE [email protected] to the actual address you want to remove from trash!\function deleteMailsFromTrash() { var gmailSearchString = `in:trash from:[email protected]` var threads = GmailApp.search(gmailSearchString); const n = threads.length; if (n