I'm running centos and have a rediculous amount of emails in the mailqueue due to a script error. Is there any way to remove messages that have a certain subject line in sendmail's mailqueue.


This worked for me just now, in testing (which is why it has such a weird subject line embedded in the code). It relies on the fact that the two files that make up a message in sendmail queue share the same filename except for the first character.

cd /var/spool/mqueue
for file in `grep "Subject:.*litigate gasworks" *|cut -f 1 -d:|cut -c2-`; do echo *${file} ; done

Once you're happy with the list of files it produces, change echo to rm. Don't forget to stop sendmail before you mess with its queue like this; it may become surly if the queue changes underneath it.


In the sendmail contrib/ directory there's a perl script qtool.pl that lets you safely make interventions on spool directories while sendmail is running. You can delete, bounce, move, and filter by age, size, sender, recipient and subject (amongst other things).

Inexplicably, it is not included with the main CentOS sendmail package, you can find it in the sendmail-doc rpm though (thanks to kubanczyk for noting this), or you can extract it from the source distribution ( ftp://ftp.sendmail.org/pub/sendmail/sendmail-current.tar.gz ). It has minimal dependencies, they should all be part of a normal perl installation.

To delete messages use -d:

qtool.pl -d -e '($msg{headers}->{Subject} =~ /whatever you need/i)' /var/spool/mqueue

To move them to an offline mqueue directory (safer):

mkdir /var/spool/mqueue-junk
qtool.pl -e '($msg{headers}->{Subject} =~ /whatever you need/i)' \
    /var/spool/mqueue-junk /var/spool/mqueue

Note it's "qtool.pl destination/ source/". Locked files (those with a "*" by the queue ID in mailq output) will not be moved, you may need to retry to catch those.

You have the option of running that queue later with sendmail -qR [...] -O QueueDirectory=/var/spool/mqueue-junk .

(Older versions of qtool.pl are known to not parse the headers correctly, you need to omit the first letter on most headers, e.g. $msg{headers}->{ubject} instead of {Subject} )

  • 1
    qtool.pl is included in CentOS sendmail-doc rpm. – kubanczyk Feb 18 '16 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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