I built an email cleaner using n8n and it's been a game-changer for my inbox

4 hours ago 2

I get a lot of emails, and I know I’m not alone. Accessing any app or site now requires an email address. Heck, even my local grocery store asked for my email and phone number. All this leads to loads of unwanted messages in my inbox because companies just can’t help but send emails about their best offers and sales.

Sure, I could unsubscribe from each one, but the clutter always finds its way back because you have to give out your email eventually. I don’t know about you, but I don’t want any promotional emails in my inbox, except maybe for those from Dbrand.

Thankfully, I was able to build an email cleaner using n8n that keeps my inbox tidy even while I sleep. It didn’t take much time either. You can set up a simple automation that deletes old emails or one that labels spam, important, promotional, alert, and other emails.

Screenshot of Apple Intelligence commercial

Related

What does my workflow look like

It works by labeling emails

n8n workflow

There’s a lot of scope for automating the email cleaning process, and a significant portion of it depends on how you want your mailbox to be organized. My n8n workflow automates Gmail organization by labeling emails based on the sender’s domain. It starts with a manual trigger (though you can also set it to run on a schedule or whenever a new email is received).

Once triggered, the Gmail node fetches a batch of recent emails from your Inbox. You can apply filters, but by default, it will retrieve the latest messages. It’s important to archive emails after processing to prevent them from being fetched again. A code snippet then extracts the domain from each sender’s email address and stores it in a new sender field. Each email’s ID is also renamed to mailID to avoid naming conflicts in later steps.

Next, the workflow uses this domain name to create a Gmail label dynamically. You can manually review and delete or archive emails based on the labels they’ve received.

The automation process takes some effort

But it's worth it

The workflow begins by authenticating your Gmail account through the Gmail node in n8n. This part took me some time since Google requires OAuth, and I kept running into errors related to the callback URL. It took some trial and error to figure that out, but once the credentials are in place, the node can pull email data directly. In this setup, a manual trigger is used to initiate the flow; however, as I mentioned earlier, you could just as easily use n8n’s email trigger to run it automatically when new messages arrive. In fact, that’s probably the better option for most use cases.

Once triggered, the Gmail node fetches a limited set of emails, ideally with a cap of 500 to avoid overloading the system. These are filtered to only include messages from your inbox. From here, a code node processes each email to extract the sender’s domain from the "From" address. It cleans and stores this domain as a new property called sender. The email ID is also renamed to mailId for clarity and to prevent conflicts in later steps.

The workflow then uses the extracted domain as a label name. If the label already exists in Gmail, the node continues smoothly. Separately, another Gmail node retrieves all existing labels, but it’s important this only runs once. If placed incorrectly, n8n will run this per email, which is unnecessary and inefficient.

With the emails and labels in hand, a Merge node combines them so that each email is matched with its respective label. This setup prepares the data for the final step, where labels are attached to messages using Gmail’s API. The label is linked using the sender’s domain and the email’s unique mailId.

After labeling, you're left with a cleaner inbox. Each email is now tagged with the sender’s domain, making it easier to bulk review, delete, or archive messages based on who sent them.

There are many more workflows to try

Throw AI into the mix

There are plenty of ways to automate inbox cleanup beyond just labeling. One simple option is to use a lightweight language model to flag emails that were likely sent by a bot rather than a human. A basic prompt, such as “Mark messages that look automated,” can already eliminate a significant chunk of clutter, likely around 90 percent of it. Even cheap models can handle this reasonably well.

To optimize for cost and performance, you don’t need to run everything through an LLM. Start with simple filters to catch the obvious stuff. For example, use basic condition checks to remove transactional messages like Google Calendar invites or any email containing an unsubscribe link. You can also filter by sender addresses using regular expressions. For instance, auto-remove emails from addresses like noreply@something.com.

For more nuanced filtering, bring in a more powerful model, but only after the basic filters have done their job. If you want to get smarter with your system, you can also track frequent senders. Use a workflow that logs sender addresses into a Google Sheet so you can spot patterns and apply custom rules, such as auto-sorting or deprioritizing messages based on how often they hit your inbox.

n8n is super useful

I started using n8n only recently, and it has quickly become one of my favorite automation tools. The no-code platform lets automate just about anything, and it’s not just for businesses. I recently built an expense tracker using n8n, and it has helped me keep my finances organized. You might also want to check these four must-have n8n automations for your home lab.

Running n8n on Proxmox

Related

4 cool automations I can pull off using my self-hosted n8n server

If you're willing to master this complex app, n8n can help you automate every aspect of your life

Read Entire Article