Gl.ib.ly

(glibly); Just another techie blog.

Deleting specific emails from the postfix mail queue

Posted by Tariq • Thursday, January 8. 2009 • Category: Computers, One liners
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.
Defined tags for this entry: , , , , , , ,
Vote for articles fresher than 90 days!
Current karma: 4.38 of 5, 8 vote(s) 13481 hits
Bookmark Deleting specific emails from the postfix mail queue  at del.icio.us Digg Deleting specific emails from the postfix mail queue Bloglines Deleting specific emails from the postfix mail queue Technorati Deleting specific emails from the postfix mail queue Fark this: Deleting specific emails from the postfix mail queue Bookmark Deleting specific emails from the postfix mail queue  at YahooMyWeb Bookmark Deleting specific emails from the postfix mail queue  at Furl.net Bookmark Deleting specific emails from the postfix mail queue  at blogmarks Stumble It!

2 Trackbacks

  1. Gerade eben musste ich wieder 1000+ EMails aus einer Postfix Mailqueue (mailq) entfernen. Weil ich mir das nicht immer neu zusammenbasteln möchte notiere ich es jetzt hier. Mit mailq | grep “test@example.com” werden die Zeilen der entsprec...
  2. From time to time things tend to go wrong and systems tend to go crazy. Sometimes these errors are more of nuisance than anything else, an intermittent annoyance you'd prefer not to investigate -- trawling through logs is a pain the backside. Now you need

3 Comments

Display comments as (Linear | Threaded)
  1. 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. 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. Works for me on Ubuntu:

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

    Thanks!

Add Comment


Standard emoticons like :-) and ;-) are converted to images.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.