Auto-delete Gmail Promotion Emails
Tags: development, flow, gmail, powerautomate
Published: 2022-02-21 03:34:34

---
title: Auto-delete Gmail Promotion Emails
published: true
date: 2022-02-05 14:25:00 UTC
tags: Development,flow,GMail,PowerAutomate
canonical_url: https://barretblake.dev/2022/02/auto-delete-gmail-promotion-emails/?utm_source=rss&utm_medium=rss&utm_campaign=auto-delete-gmail-promotion-emails
---

* * *

Cleaning up junk mail is an ongoing tribulation. Gmail does a pretty good job of identifying emails that are promotional in nature and flagging them as such. But over time those promo emails can eat up your email space and leave you with a mess to clean up. Thankfully, Power Automate can help us keep those promotions emails in check.

## Creating Our Trigger

Unfortunately, the Gmail connector for Power Automate doesn’t let you list emails in your mailbox. The only thing you can do is react when a new email arrives. So while it won’t help you clean up your existing mess, we can set up a flow to deal with the new emails going forward. For this flow, we’re going to select the Gmail “When a new email arrives” trigger.

![gmail-new-email-trigger](http://barretb1.sg-host.com/wp-content/uploads/2022/02/gmail-new-email-trigger.png)
_gmail-new-email-trigger_

The trigger does allow us to filter which emails will trigger the flow. In this particular case, we only want to trigger the flow when an email gets the Promotions label. So in the trigger, under the Label parameter, select “Category/Promotions”. If you want to further filter the emails, you can provide other options. But for this purpose we’re just interested in the Promotions label.

![gmail-trigger-promotions](http://barretb1.sg-host.com/wp-content/uploads/2022/02/gmail-trigger-promotions.png)
_gmail-trigger-promotions_

## Now It’s Time To Wait

After the trigger occurs, we don’t want to do anything else for a period of time. The trigger provides us with the key piece of information, the ID of the email message that came in. Now we want to wait? Why? Well, we need to give ourselves time to review the email and decide if we want to keep it. So, after the trigger, let’s add a “Delay” action and give it a delay time. For this flow, I’ll put in a value of 8 days. That should provide me ample time to check emails and save the ones I want to keep before it gets deleted.

![gmail-delay](http://barretb1.sg-host.com/wp-content/uploads/2022/02/gmail-delay.png)
_gmail-delay_

## Time’s Up

Once the email has marinated for a bit, our next step is to check the email again to see if we want to delete it. So our next step is to retrieve the email details once more so we can check the label list. We’ll next call the Gmail action “Get email details”. We’ll pass in the ID from the trigger. This will let us check and see if you have removed the Promotions label from the email during the delay period.

![gmail-get-details](http://barretb1.sg-host.com/wp-content/uploads/2022/02/gmail-get-details.png)
_gmail-get-details_

Among the outputs from the details action is an array of current labels on the email. If you wanted to save the email, during that delay period you will have marked the email as not being a promotion, hence removing the tag.

Thankfully, Power Automate has a handy function that we can use to check and see if the Labels array contains the Promotion value we’re looking for. So let’s add a Condition action after our email details. Into the left box of our condition, we’re going to add the following expression:

```
contains(outputs('Get_email_details')?['body/LabelIds'], 'CATEGORY_PROMOTIONS')
```

This will check the LabelIds array that is returned from the “Get email details” action to see if one of the values is equal to ‘CATEGORY\_PROMOTIONS’. This is the label ID Gmail uses to identify promotions email. In the right box we’ll add ‘true’ as the value we’re checking for.

If it is true, we want to delete the email. So in the “If yes” box, we’ll add a Gmail “Delete email” action. That will send our email to the garbage bin of eternity, to be tossed with the next emptying. And that’s all there is too it.

![gmail-condition-delete](http://barretb1.sg-host.com/wp-content/uploads/2022/02/gmail-condition-delete.png)
_gmail-condition-delete_

Download the full flow here

[Auto Delete Gmail Promotions](http://barretb1.sg-host.com/wp-content/uploads/2022/02/AutoDeleteGmailPromotions_20220206002137.zip)[Download](http://barretb1.sg-host.com/wp-content/uploads/2022/02/AutoDeleteGmailPromotions_20220206002137.zip)

The post [Auto-delete Gmail Promotion Emails](https://barretblake.dev/2022/02/auto-delete-gmail-promotion-emails/) first appeared on [Barret Codes](https://barretblake.dev).