How to clean Postfix queue from spam mails

If you have a lot of Postfix queue but there are some legitimate mails too, you can run the following command. This will remove mails going to a specific domain.

# mailq | tail -n +2 | head -n 100000 \
| grep -v "delivery temporarily suspended" \
| awk 'BEGIN { RS = "" } \
# $7=sender, $8=recipient1, $9=recipient2 \
{ if ($8 ~ /yahoo.com.tw/) \
print $1 } ' \
| tr -d ’*!’ | postsuper -d -

The command above only parses the first 100 000 rows of your queue. This is for giving you fast response if your search criterias gave any result.

To run search the whole queue, remove “| head -n 100000”.