Have you ever done Postfix delete mail queue operation?

Postfix is rich in features and easy to handle too. Mail queues of Postfix stores each email we send and records its success/failure.

At Bobcares, we often receive requests regarding Postfix mail queue operations as part of our Server Management Services.

Today, let’s discuss the deletion of mail queue in Postfix and how our Support Engineers do it efficiently.

About Postfix mail queue

As we all know, the Mail Transfer Agents(MTA) highly affect the deliverability of emails.

And, Postfix is a security-oriented MTA that provides a high level of flexibility and ease of administration.

Also,  proper configuration of Postfix MTA provides improved defenses against spam, abuse, and leakage of sensitive data.

By default, Postfix maintains two mail queues: pending and deferred.

The pending queue contains mails that are not sent to the remote server. And, the Deferred queue holds mails that had a temporary failure and needs a retry.

Moreover, the Postfix mail server will retry sending the deferred queue mails on set intervals.

Postfix delete mail queue

So far we have discussed the Postfix mail queue. Now, let’s get into its deletion operation quickly.

The postqueue command implements the Postfix user interface for queue management.
Before you can flush, purge, or otherwise manage your mail queue, you first need to look at which emails are currently sitting inside your mail queue.

Usually, to view the number of emails in the Postfix mail queue, we use the command:

mailq

OR
postqueue -p

OR

You can view your mail queue using the postqueue -p command.

mailhost01:~ user01$ postqueue -p

The results should resemble the following.
-Queue ID- –Size– —-Arrival Time—- -Sender/Recipient——- 9DJF64JS935J* 319 Fri Dec 27 20:24:06 sender@test.com recipient@test.com 78J3EIS0FH45* 319 Fri Dec 27 20:24:17 sender@test.com recipient@test.com

The symbols at the end of the Queue ID provide information about the status of the email.

  • An exclamation mark (“!”) notates that the queue status is on hold.
  • An asterisk (*) notates that the queue status is active.

Examine the queue and determine if you would like to flush or purge it.

Recently, one of our customers approached us to delete all the mails in his mail queue.

Therefore, to clear the mail queue in Postfix, our Support Engineers used postsuper command. Postfix superintendent is its abbreviation.

Purge, Flush, or Delete the Postfix Mail Queue

If you need to clear or retry messages in your Postfix mail queue, follow these steps.

First, check the current mail queue:

mailq

To flush or clear queued messages, run:

postfix flush

If you want to retry all deferred messages, use:

postsuper -r ALL
Purging the Mail Queue

Purging lets you permanently remove emails from the queue. You can use the postsuper -d command in two ways:

Delete a specific message:

postsuper -d [message id]

Delete all messages:

postsuper -d ALL

Remove a single email:

mailhost01:~ user01$ postqueue -p
-Queue ID- –Size– —-Arrival Time—- -Sender/Recipient——-
9DJF64JS935J* 319 Fri Dec 27 20:24:06 sender@test.com recipient@test.com
78J3EIS0FH45* 319 Fri Dec 27 20:24:17 sender@test.com recipient@test.com
mailhost01:~ user01$ sudo postsuper -d 9DJF64JS935J
postsuper: 9DJF64JS935J: removed
postsuper: Deleted: 1 message

Remove all queued emails:

mailhost01:~ user01$ postqueue -p
-Queue ID- –Size– —-Arrival Time—- -Sender/Recipient——-
9DJF64JS935J* 319 Fri Dec 27 20:24:06 sender@test.com recipient@test.com
78J3EIS0FH45* 319 Fri Dec 27 20:24:17 sender@test.com recipient@test.com
mailhost01:~ user01$ sudo postsuper -d ALL
postsuper: Deleted: 2 message

You can safely repeat this purge whenever needed without impacting server performance.

Flush the Postfix Mail Queue

In older Sendmail setups, the queue was flushed using:

sendmail -q

With Postfix, use any of the following commands instead:

postfix flush
OR
postfix -f
## OR ##
postqueue -f

These commands immediately process and send all pending messages in the queue.

The postsuper command

Usually, we use this command in four scenarios.

So, to delete all emails in the queue, we use this command as:

postsuper -d ALL

Also, to remove all mails in the deferred queue, the command is as follows:

postsuper -d ALL deferred

Similarly, we can remove a particular mail from the queue using the command,

postsuper -d mail_id

And, we get the mail queue ID on running the mailq command.

To remove messages from a particular domain.

mailq | grep domain.com | awk {'print $1'} | xargs -I{} postsuper -d {}

Therefore, using these commands, we can perform different deletion operations in the Postfix mail queue.

postfix-delete.pl script

The following script deletes all mail from the mailq which matches the regular expression specified as the first argument (Credit: ??? – I found it on old good newsgroup)
#!/usr/bin/perl

$REGEXP = shift || die "no email-adress given (regexp-style, e.g. bl.*\@yahoo.com)!";
@data = qx</usr/sbin/postqueue -p>;
for (@data) {
if (/^(\w+)(\*|\!)?\s/) {
$queue_id = $1;
}
if($queue_id) {
if (/$REGEXP/i) {
$Q{$queue_id} = 1;
$queue_id = "";
}
}
}
#open(POSTSUPER,"|cat") || die "couldn't open postsuper" ;
open(POSTSUPER,"|postsuper -d -") || die "couldn't open postsuper" ;
foreach (keys %Q) {
print POSTSUPER "$_\n";
};
close(POSTSUPER);
For example, delete all queued messages from or to the domain called fackspamdomain-dot-com, enter:
./postfix-delete.pl fackspamdomain-dot-com
Delete all queued messages that contain the word “xyz” in the e-mail address:
./postfix-delete.pl xyz

Flush The Mail Queue

In order to flush your mail queue, you need to use the post queue -f command.

mailhost01:~ user01$ postqueue -f

When you use the flush command, the queue will attempt to redeliver all the emails remaining within it. Be wary of using the flush command too often, as multiple flushes can negatively affect your mail server’s performance.

[Need more help in mail queue operations?- We’ll help you.]

Conclusion

In short, we perform the postfix delete mail queue operation using postsuper command. In today’s writeup, we discussed the Postfix mail queue and saw how our Support Engineers perform deletion operation successfully.

FAQ’s

1. What is a Mail Transfer Agent (MTA)?
An MTA routes and delivers electronic mail between email servers, processing and forwarding messages. Postfix is a popular MTA.
2. How can I integrate Postfix with other email services and tools?
Configure Postfix to work with services like Iron, SendGrid, and ActionMailer by editing configuration files and following integration instructions.
3. What are the primary components of Postfix architecture?
Key components include the SMTP server, SMTP client, queue manager, and the local delivery agent.
4. How can I control the amount of email sent through Postfix?
Configure settings in the main configuration file to set limits on connections, messages per session, and individual message size.