Routing the world

Routing & IT System Administration

Delete messages from Postfix Queue based on from or to addresses

leave a comment »

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.

Advertisements

Written by Root

April 24, 2013 at 09:00

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: