Deleting specific emails from the postfix mail queue

Save on DeliciousDigg This
Share on Facebook+1Share on LinkedInPin it on PinterestSubmit to redditSubmit to StumbleUponShare on TumblrShare on Twitter

A week ago a development database server lost power, which is usually no big deal only that I have some reporting scripts that run every 5 minutes for each database. When these databases became unavailable they (the scripts) like to send me a quick mail telling me what went wrong. The network switch also was out of action so there was no route to the outside world from this small development network. The server lost power for three days and a massive amount of mail built up in the mail queue. When power was restored the SPAM started pouring in.

As I started to receive the first wave of mails I logged into the server a did the typical sudo mailq | wc -l which showed 13,000 emails waiting to make their way down the ether. Unfortunately I couldn’t just delete all mail on this server so I had to think up a quick way to delete only the stuff I needed to; however, luckily, all my outgoing mail was sent from a unique address database-reporter@my.dev.box. Using this information I came up with the following one liner (in all its unedited glory).

# Executed as root
mailq | grep "database-reporter@my.dev.box" | awk '{ system("/usr/sbin/postsuper -d "$1); }'

Now there are about a gazillion ways to do this (many are better) but I needed something which would stop those mails flying about the place before I got blacklisted and it worked, so there.mailq gets me a list of emails in postfix’s mail queue; grep gets me only the mails I am interested in; awk pulls out the mailID and uses it to issue a command to delete mail from the mailqueue with that id; simple.One better way to do this line could be:

# Executed as root
 mailq | grep "database-reporter@my.dev.box" | awk '{ print($1); }' | /usr/sbin/postsuper -d -

This should be a lot quicker, as postsuper will read all the piped IDs and delete them as they arrive at STDIN. There is some good info in the man postfix pages under the -d option.

A quick GFI came up with Deleting mail in postfix queue (mailq) which was about all the reference I needed.

Save on DeliciousDigg This
Share on Facebook+1Share on LinkedInPin it on PinterestSubmit to redditSubmit to StumbleUponShare on TumblrShare on Twitter
Tagged with: , , , , , , ,
Posted in Stuff
3 comments on “Deleting specific emails from the postfix mail queue
  1. Christian says:

    This is awesome and just what I needed, thanks.
    For reference, my mailq included an asterisk on the end of the mail id;
    S3F5GAR445*
    So I used the following adaptation of your script;
    mailq | grep “email_address” | awk ‘{ print(substr($1,1,10)); }’ | /usr/sbin/postsuper -d -

  2. tariq says:

    Cool, thanks for the feedback! You can also use ‘sed s/*//’ to filter out pesky asterisks or even grep:

    mailq | grep ‘email_address’ | grep -o ‘^[^*]*’ | /usr/sbin/postsuper -d -

  3. Dan says:

    Works for me on Ubuntu:

    mailq | egrep -o “^[0-9A-F]*” | postsuper -d -

    Thanks!

Leave a Reply to Dan Cancel reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>