Quick tip for managing postfix’s mail queues. A customer uses an old mailing software and he got subscription of unknown domains, which then remains in queue for long time. To purge all mails destinated to a single domain from your mail queue use the following command:
mailq | grep -B3 "\.ovh$" | grep "^[0-9A-Z]" | cut -f 1 -d ' ' | xargs -n 1 postsuper -d
this will delete all mails in queue for the recipient TLD .ovh.
Explaination:
- mailq shows the mail queue;
- grep is used to pick the lines for our selected domain. The -B3 option means to also show three lines before the matched one;
- grep once more is to select only the rows starting with a postfix queue ID;
- cut is to pick only the first word of the line, which is the postfix queue ID. We could have used awk {print $1} as well;
- xargs is to run a command against a list of elements;
- postsuper -d is for finally deleting an element from the postfix queue.