28 September, 2016

Delete messages from Postfix Queue based on from or to addresses

When you need to delete messages from a Postfix Queue based on the From or To headers you can do it using the next “one-liner” commands:
  • For message deletion in a Postfix Queue based on From address
mailq | tail -n +2 | grep -v '^ *(' | awk 'BEGIN { RS = "" } { if ($7 == "address@mail.example.com" && $9 == "") print $1 } ' | tr -d '*!' | xargs rm
  • For message deletion in a Postfix Queue based on To address
mailq | tail -n +2 | grep -v '^ *(' | awk 'BEGIN { RS = "" } { if ($8 == "address@mail.example.com" && $9 == "") print $1 } ' | tr -d '*!' | xargs rm
What changes between the two commands is the position the awk command get to make parsing in order to obtains the MSGID before push it to delete command.
You could change "xargs rm" to "wc -l" to get the number of messages affected by the message group based on the From or To address. This must be done ALWAYS BEFORE delete the messages from the queue.
UPDATE 24/04/2013: If you are using a modern version of Postfix with queue in a complex directory structure, you have to change the last argument “xargs rm” to “xargs -rn1 postsuper -d“. This is done because postsuper command can delete messages from queue in all that complex structure and you don’t need to enter each directory to be able to delete the files.

No comments:

Post a Comment

Redirection in IIS

 This config will redirect every request to https://www.domain.com <configuration>     <system.webServer>         <rewrite>...