Delete MAILER-DAEMON emails from the postfix queue

I often am faced with deleteing mails from MAILER-DAEMON that are going to non existant domains in my Postfix queue. Rather than manually deleting each email by hand, I now use a little script that does the job for me.

All I do now is type in “delmd” without the quotes and all the emails in the postfix queue from MAILER-DAEMON are deleted.

I save the script as delmd in /bin and it needs to be executeable ( I use 0755), the script is below.

#!/bin/bash
mailq | tail -n +2 | grep -v ‘^ *(‘ | awk ‘BEGIN { RS = “” }
# $7=sender, $8=recipient1, $9=recipient2
{ if ($7 == “MAILER-DAEMON” )
                      print $1 }
‘ | tr -d ‘*!’ | postsuper -d –

Before I run delmd I just double check the queue using mailq to make sure that no legitimate emails from MAILER-DAEMON are in the postfix  queue. I always find this to be a very rare occurence.

I run this script on a Centos based Linux system, I will not be liable for any problems that may arise from you using this script.