Let’s say you’re on an email thread that you no longer want to be on, but you’re too nice to communicate that. You just don’t want to get any emails from that group any more. Thanks to Gmail filters, there’s a way you can programmatically achieve this without a bunch of repetitious elbow grease.1 First, create a JavaScript file and open VSCode:

cd ~/Desktop
touch file.js
code file.js

Next, get your list of email addresses from which you no longer wish to receive, and format them in a way that JavaScript can understand. Using some simple search and replace in your text editor, it shouldn’t be too hard. The goal is for you to get it in an array format:

const addresses = ["Example User 1 <[email protected]>", "Example User 1 <[email protected]>"];

Next, put this into your newly created file.js, with that formatted version at the top:

const addresses = ["Example User 1 <[email protected]>", "Example User 1 <[email protected]>"];

let startTime = new Date().getTime();
const filters = addresses.map(address => {
  const start = address.indexOf('<') + 1;

  return {
    time: startTime++,
    email: address.substr(start, address.length - start - 1)
  }
});

const template = `<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>
<title>Mail Filters</title>
<id>tag:mail.google.com,2008:filters:${ filters.map( el => el.time ).join(',') }</id>
<updated>2018-08-04T14:41:59Z</updated>
<author>
  <name>[Your name]</name>
  <email>[Your email]</email>
</author>
${ filters.reduce((acc, el) => `${acc}
<entry>
<category term='filter'></category>
<title>Mail Filter</title>
<id>tag:mail.google.com,2008:filter:${el.time}</id>
<updated>2018-08-04T14:41:59Z</updated>
<content></content>
<apps:property name='from' value='${el.email}'/>
<apps:property name='shouldTrash' value='true'/>
<apps:property name='sizeOperator' value='s_sl'/>
<apps:property name='sizeUnit' value='s_smb'/>
</entry>`, '') }
</feed>`;

template;

Replace the [Your name] and the [Your email] with your name and email address.

Now copy the contents of this file to your clipboard:

pbcopy < file.js

Then head over to Chrome, pop open the JavaScript console, and paste your clipboard. Copy the string output stored in the template variable, and paste it into a new file:

pbpaste > import.xml 

Now go into Gmail > Settings > Filters and Blocked Addresses, scroll to the bottom, and select “Import filters.” Choose import.xml. Complete the steps, and you’re done.

I just did this successfully with a list of 75 email addresses and it felt amazing.


  1. Gmail’s interface allows you to create filters directly, but there’s a severe character limit when you’re assembling the filter. If you have a list of more than a few email addresses that you’re wishing to filter, you’re out of luck. That’s where this tutorial comes in. ↩︎