How to remove Postfix queue messages sent to a specific domain

I have a server with multiple domains. How can I clear all Postfix queue messages for a specific domain?


Solution 1:

UPDATE 2021-04-18:

mailq | tail -n +2 | grep -v '^ *(' | awk  'BEGIN { RS = "" } { if ($8 ~ /@example\.com/ && $9 == "") print $1 }' | tr -d '*!' | postsuper -d -

Whereas $7=sender, $8=recipient1, $9=recipient2. You can also adapt the rule for other recipients ($9) to your needs.

The command is based on an example of the postsuper manpage which an example command matching a full recipient mail address:

mailq | tail -n +2 | grep -v '^ *(' | awk  'BEGIN { RS = "" } { if ($8 == "[email protected]" && $9 == "") print $1 }' | tr -d '*!' | postsuper -d -

Old content:

This command deletes all mails sent from or to addresses that end with @example.com:

sudo mailq | tail -n +2 | awk 'BEGIN { RS = "" } /@example\.com$/ { print $1 }' | tr -d '*!' | sudo postsuper -d - 

Solution 2:

I have tried this solution in ubuntu 12.04, and it doesn't work this way:

sudo mailq | tail +2 | awk 'BEGIN { RS = "" } / @example\.com$/ { print $1 }' | tr -d '*!' | sudo postsuper -d -

I need to change to this way:

postqueue -p | tail -n +2 | awk 'BEGIN { RS = "" } /@example\.com/ { print $1 }' | tr -d '*!' | postsuper -d -

Solution 3:

Grep solution

mailq | grep example.com -B1 | grep -oE "^[A-Z0-9]{10,11}" | sudo postsuper -d -

assumes ID is between 10 and 11 digits, (based on inodes)

Solution 4:

Look at pfdel.pl, a mandatory tool to manage the queue. It takes a regexp and remove the mails waiting in queue corresponding to your domain.