Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a mailq which is getting backed up with multiple emails with the same subject line. I would like to delete all emails in the mailq that match a particular subject line so they don't get sent. Any ideas how to do this? Pretty urgent as its spam related.

share|improve this question
what MTA are you using ? – topdog Jun 17 '11 at 9:38
I'm using postfix – David Jun 17 '11 at 9:39
up vote 2 down vote accepted

Postfix does not have a utility like exigrep, so you will need to grep the queue files for the subject and then pipe the queue id to postsuper to delete them

share|improve this answer

With a typical postfix installation the email will be in /var/spool/postfix. There are several queues. You want to stop postfix so that you can safely use postsuper to remove the emails. This short script will remove all the emails that match a particular string. In our case we needed to find thousands of emails that all had the same subject line.

In this case, the emails were all deferred, because our remailing service had rejected them due to our being over the limit.

cd /var/spool/postfix/deferred
grep -r -i -l "This was the subject line" ./ | cut -d/ -f3 | postsuper -d -

Some key notes on this:

  • grep -l returned the filename for matches, which is the queueid of the matched message
  • the messages were all in different subdirs so the cut was to strip the path off the front. Make sure you test your return path to insure you're just getting the queue name
  • postsuper -d - tells postsuper to delete messages it got from stdin.

Hope this helps people who find this and are looking for more specific instructions.

share|improve this answer
Unfortunately, this approach does not work for Unicode subject lines (means, those using encoded-word tokens as per RFC2047). But even grep-ing through postcat output does not work in these cases. – tanius Sep 14 '14 at 21:26

Just an alternative command to do the same proposed by gview:

find /var/spool/postfix/deferred/ -exec grep -l 'Subject: this was the subject line' {} \; | xargs -r -n1 basename | xargs -r -n1 postsuper -d
  • find + grep -l: find the deferred emails on file system with the given subject
  • xargs + basename: retrieve the message ID from the path of the mail file
  • xargs + postsuper: use the retrieved message ID to feed postsuper -d and delete the mail from the queue
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.