DEV Community

Barret Blake
Barret Blake

Posted on • Originally published at barretblake.dev on

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
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
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
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
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')
Enter fullscreen mode Exit fullscreen mode

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
gmail-condition-delete

Download the full flow here

Auto Delete Gmail PromotionsDownload

The post Auto-delete Gmail Promotion Emails first appeared on Barret Codes.

Top comments (0)