Deleting Emails From a Mail Server
  • 18 Nov 2020
  • 5 Minutes to read
  • Dark
    Light
  This documentation version is deprecated, please click here for the latest version.

Deleting Emails From a Mail Server

  • Dark
    Light

Overview

Flows can download, list, and delete email messages from either an IMAP or POP mail server. The Delete component can be configured to delete emails that meet certain criteria. A Flow can delete all emails sent by one sender, the first or last email in an account, a set number of emails, unread emails, or even all emails for the account on the server.

The Delete component uses the unique id for an email as input, then sends the command to a mail server for the messages that meet the criteria to be deleted. To delete emails from a mail server, in the Steps panel, in the category Communication > IMAP, drag a Delete component to the workspace, and configure it in the Mapping Editor.

This example will work for a POP mail server also. The steps to use for a POP mail server are located in the Steps panel, under Communications > POP. 

Example

The example Flow will delete the first unread email from an existing Gmail account. The email that will be deleted is sent from Ajay Pawar, and has the subject "Test." The process for deleting many hundreds of messages according to carefully constructed rule sets is not much different from the one in the example.

Begin by navigating to a project folder and clicking the Create Flow button. Give the flow a name and select CREATE to open it in the Flow Designer.

The first step will be to add the Get Headers component, which will retrieve the message headers from the inbox of an email account. These headers tell the Delete command which email to delete. 

From the Flow Designer's startup window, in the category All Steps [Catalog] > Communication > IMAP, select a Get Headers component. Select Add to add it to the workspace.

Set values in the Properties Panel to tell the Get Headers component how to access the email server.

To collect the message headers, the Get Headers component must know how to access the mail server. Configure Get Headers similar to configuring an email client like Outlook, Thunderbird or Apple Mail. Also, configure Get Headers to retrieve only a specified number of messages or messages that are unread.

Enter information about the email account and server in the Value fields in the Server, Port, Username, and Password sections. Because we only want to delete one email, we enter "1" in the Value field in the Count section.

Also select the Value checkbox in the Secure Connection section, indicating that the flow should access the email server using a secure connection.

Finally, by selecting the Value checkbox in the Unread Messages Only section, set the flow to delete only a message that is unread.

Once the Get Headers step has retrieved the messages from the Gmail account, the next step is to delete the first unread email message.

In the Steps panel, in the category Communication > IMAP, drag a Delete component to the workspace.
2019-07-01_1650.png


The next step is to configure Delete 1 to communicate with the mail server, like the Get Headers component.

Enter information about the email account and server in the Value fields in the Server, Port, Username, and Password sections.

Select the Value checkbox in the Secure Connection section, indicating that the flow should access the email server using a secure connection.

One additional field that's required for the Delete component is the Unique Id. This unique id may refer to one message or many messages, but it is the key that will be used to determine which messages are deleted.

Expand the outputs available from Headers, view a wide variety of options, each of which offers some refinement of the entire collection. Whichever one mapped to Unique Id will determine what messages are deleted on the server.

To map the output All Sender to the input Unique Id and then enter "ted@example.com" in the Value field, the step would be configured to command the mail server to delete all messages which were sent by "ted@example.com."

To map the output All IsDeleted to the input Unique Id, the step would be configured to command the mail server to delete all messages that have been deleted (i.e. messages that have been moved to the Trash folder).

In this example, delete the first unread message in the email account, so expand the input All UniqueId. This input correlates to all messages in all folders.

If the unique identifier for the message to delete is known, as it is represented on the mail server, map the output All UniqueId directly to the input Unique Id and enter that unique identifier into the Value field, where it might look something like: "Qnrz3VhGe232h2SX092bAdL". 

Because this information may not be available, All UniqueId offers three preset inputs to select the most common range of messages, even when the unique identifier is unknown. First, select the first message in the collection of headers. Last will select the last. The count will select a number of messages from the top of the stack.

The requirements specify that to delete the first unread message, map the output Headers > All UniqueId > First to the input Unique Id.

Debug

Select Debug in the top action panel of the Flow Designer.

Because the Flow interacts with external systems, view the lag time between our flow and Gmail's servers reflected in real-time in the debugger's events and on the Diagram tab. This view is by no means a perfect indicator of network health, but if there is a significant lag between steps, it might be an indicator of a problem worth exploring. The lag-time seen while debugging is perfectly normal.

To confirm the Flow deleted the first unread email, access the account. The account contains 3 unread messages. The first unread message received in the account displays at the bottom of the list with the subject "Test."


The email was open when the Flow ran, select Refresh.


See that the Flow deleted the first unread email message. The Inbox now indicates that there are 2 unread emails, and the email with the subject "Test" no longer displays. By using the Get Headers and Delete components in sequence, the Flow has successfully deleted the first unread message from the email account.



Was this article helpful?