Manage the Postfix mail queue with postsuper, postqueue and mailq commands

Postfix provides several shell programs to manage the mail queue. These are:

  • postsuper
  • postqueue
  • mailq

In this tutorial, I will show you how to use these commands to manage the Postfix mail queue. Here are examples for common tasks:

List all messages that are in the mail queue

postqueue -p

The output looks like this:

root@server:/# postqueue -p
 -Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
 501CA23B43DB     2182 Thu Dec  3 14:24:39  test@yourdomain.com
 (Host or domain name not found. Name service error for name=otherdomain.com type=MX: Host not found, try again)
 info@otherdomain.com
-- 8 Kbytes in 1 Requests.

Delete a message by message ID from Postfix mail queue

postsuper -d MessageID

replace MessageID with the ID of the message, e.g. 501CA23B43DB

root@server:/# postsuper -d 501CA23B43DB
 postsuper: 501CA23B43DB: removed
 postsuper: Deleted: 1 message

Remove emails by sender from Postfix mail queue

mailq | tail +2 | awk 'BEGIN { RS = "" }
 # $7=sender, $8=recipient1, $9=recipient2
 { if ($7 == "info@otherdomain.com" && $9 == "")
 print $1 }
 ' | tr -d '*!' | postsuper -d -

replace "info@otherdomain.com" with the sender's email address. In case you get a syntax error from the tail command, use this syntax:

mailq | tail -n +2 | awk 'BEGIN { RS = "" }
# $7=sender, $8=recipient1, $9=recipient2
{ if ($7 == "info@otherdomain.com")
print $1 }
' | tr -d '*!' | postsuper -d -

Remove emails by recipient from Postfix mail queue

mailq | tail +2 | awk 'BEGIN { RS = "" }
 # $7=sender, $8=recipient1, $9=recipient2
 { if ($8 == "you@yourdomain.com" && $9 == "")
 print $1 }
 ' | tr -d '*!' | postsuper -d -

replace you@yourdomain.com with the recipient's email address.

In case you get a syntax error from the tail command, use this syntax:

mailq | tail -n +2 | awk 'BEGIN { RS = "" }
# $7=sender, $8=recipient1, $9=recipient2
{ if ($8 == "info@otherdomain.com")
print $1 }
' | tr -d '*!' | postsuper -d -

Remove all emails from the Postfix mail queue

To remove all emails from the mail queue, use this command:

postsuper -d ALL

Remove emails by sender hostname from Postfix mail queue

mailq | grep senderhostname | awk ‘{ print $1′} | postsuper -d -

replace the word "senderhostname" with the hostname of the email sender.

If your server has a very high load and you want to temporarily move all message from the incoming queue to the hold queue, use the command:

postsuper -h ALL

to move the messages back to the incoming queue, use the command:

postsuper -r ALL

Instead of the word "ALL" you can also provide a specific message ID to move only one message to or from the hold queue. Messages in the hold queue will not be processed by postfix until they were re-queued with "postsuper -r" command.