DEV Community

wasteofserver
wasteofserver

Posted on • Originally published at wasteofserver.com on

Permanently delete unwanted emails from Gmail. Out of sight, out of mind!

Permanently delete unwanted emails from Gmail. Out of sight, out of mind!

This post was originally posted on https://wasteofserver.com/permanently-delete-unwanted-emails-from-gmail-out-of-sight-out-of-mind/, you will find newer revisions and additional comments there.

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 lawyer
  • Delete those emails and purge them from trash, bypassing Gmail's 30-day retention policy

Permanently delete unwanted emails from Gmail. Out of sight, out of mind!
Out of sight, out of mind can be the best policy.

Create a forward address

On 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".

Permanently delete unwanted emails from Gmail. Out of sight, out of mind!
Remember to keep forwarding DISABLED, we only want to forward emails from a single sender

After 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 Filter

Here 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".

Permanently delete unwanted emails from Gmail. Out of sight, out of mind!

The form for adding filters will open. You want all messages from that specific address to be filtered, so just add "someone@example.com" to the filter and choose "Create filter".

Permanently delete unwanted emails from Gmail. Out of sight, out of mind!
You can be picky with filters, but here we want ALL emails from sender to match

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.

Permanently delete unwanted emails from Gmail. Out of sight, out of mind!
And just like that, emails from this sender are forwarded and trashed!

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 Script

Google 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".

Permanently delete unwanted emails from Gmail. Out of sight, out of mind!
Hurray for your first script!

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.

Permanently delete unwanted emails from Gmail. Out of sight, out of mind!
Your script needs the Gmail API to access your emails

Now, replace the myFunction with this piece of code. Remember, YOU MUST CHANGE someone@example.com to the actual address you want to remove from trash!

function deleteMailsFromTrash() {
  var gmailSearchString = `in:trash from:someone@example.com`

  var threads = GmailApp.search(gmailSearchString);
  const n = threads.length;
  if (n <= 0) {
    Logger.log("No threads matching search string \"%s\"", gmailSearchString);
    return
  } else {
    Logger.log("%s threads matching action **%s**", n, gmailSearchString);
  }

  for (var i = 0; i < threads.length; i++) {
    var thread = threads[i];
    Logger.log(`\t Thread# ${i} [ID: ${thread.getId()}]: [message : ${thread.getFirstMessageSubject()}] deleted`);
    Gmail.Users.Threads.remove('me', thread.getId());
  }
}
Enter fullscreen mode Exit fullscreen mode

This script will look in the trash for emails from someone@example.com and delete them

Your screen should now resemble this. Go ahead and rename "Untitled project" to something more meaningful, like "Purge Specific Mails from Trash". Also change myFunction to deleteMailsFromTrash and then hit Run.

You'll be asked to give permissions to access your Google account.

Permanently delete unwanted emails from Gmail. Out of sight, out of mind!

Now you'll get an error! Google hasn't verified this app. While the developer hasn't verified the app, you shouldn't use. In this particular case, you are the developer! That's why I didn't release this solution as a pre-made script. It's safer to have the code running on your side.

Permanently delete unwanted emails from Gmail. Out of sight, out of mind!

Click on that "Go to Purge Specific Mails from Trash (unsafe)" link and then continue. On your Apps Script window, you'll see the first execution of your script.

On my case, since I don't have email from someone@example.com on the "trash" the program simply prints a "No threads matching search string". On your specific case, you may see that a couple of emails have been deleted! Well done.

Permanently delete unwanted emails from Gmail. Out of sight, out of mind!

Now everything is working, but we still need to set up a trigger to run the script automatically, ensuring unwanted emails are deleted in a timely manner.

Configure a trigger!

You'll want a time driven trigger. Something that runs on a schedule making sure that the emails that were placed in the trash by the filter you created above are purged before you can get to them.

Click on the clock on the left sidebar, then on the big blue button on the bottom right that says "Add Trigger" and configure the filter as per the image below.

Permanently delete unwanted emails from Gmail. Out of sight, out of mind!
And there you have it! Process is automated.

I’ve set this script to run every 5 minutes, but you can adjust the interval to as low as 1 minute if you prefer. Tweak as needed. Setting a longer interval is simply a way to considerate of Google's infrastructure.

Now, to make sure things are working as expected, on the left sidebar, click on executions. You'll see a table with all the times the script has run. As you've just implemented it, there should probably be two to three runs. One manual Type: Editor from when you manually executed it and then a couple more from the time-driven trigger labeled with Type: Time-Driven.

Permanently delete unwanted emails from Gmail. Out of sight, out of mind!
Peace.

As challenging as it may be to navigate these situations, it’s important to remember that protecting your health is a priority.

While technology can help us minimize harmful distractions, healing takes time and self-compassion. Stay strong, take care of yourself, and don’t hesitate to seek support.

You deserve peace and healing through this process.


Permanently delete unwanted emails from Gmail. Out of sight, out of mind!

I don’t know if you’ve tried rucking before, but it’s something that liberates my mind.

It’s simply walking while carrying weight. Originally a military exercise, rucking is gaining popularity for its benefits to physical health, stability, and mental well-being. Give it a try!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more