Added on Mar 10th, 2015 and marked as mail queue server

Postfix stores messages in a mail queue before actually sending it. Sometimes a message can’t be sent and in that case you can access the queue and remove it manually.

Display the messages in the queue

To see which messages are currently in the queue:

mailq

This will result in something like this:

-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
BF74A87146      333 Tue Mar 10 08:30:45  root@example.com
                                                           (temporary failure)
                                         test@example.com

-- 0 Kbytes in 1 Request.

In this example a mail from [email protected] to [email protected] got temporarily stuck in the queue.

Flush the queue

To flush the mail queue under postfix you simply do this command:

postfix flush

This will process the queue, trying to deliver the remaining messages. If the message is not delivered but requeued instead, it is time to check the logs for any error messages.

Remove queued messages

Single message

If you just need to remove a single message, this is the command you need:

postsuper -d MAILID

where MAILID is the ID of the mail in the queue.

All messages

To clean up the queue completely, you can remove the messages using this command:

postsuper -d ALL

Selective

This is a script floating around the internet for who knows how long. It will delete only the messages that match the specified regular expression.

delete-from-mailqueue.pl
#!/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,"|postsuper -d -") || die "couldn't open postsuper" ;
foreach (keys %Q) {
print POSTSUPER "$_\n";
};
close(POSTSUPER);

The following commands will delete any message that contains either example.com or root in the e-mail address:

./delete-from-mailqueue.pl example.com
./delete-from-mailqueue.pl root