Question: I'm using Exim mail server under CentOS Linux. How do I remove all messages from the Exim mail queue using a shell prompt?
Answer: Exim is a mail transfer agent (MTA) used on Unix-like operating systems. It aims to be a general and flexible mailer with extensive facilities for checking incoming e-mail.
To print a list of the messages in the queue, enter:
# exim -bp
To remove a message from the queue, enter:
# exim -Mrm {message-id}
To remove all messages from the queue, enter:
# exim -bp | awk '/^ *[0-9]+[mhd]/{print "exim -Mrm " $3}' | bash
Dallas Marlow, suggested following clean command:
# exim -bp | exiqgrep -i | xargs exim -Mrm
Further readings:
- man pages awk, exim
- Exim documentation
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- My 10 UNIX Command Line Mistakes
- Linux: 20 Iptables Examples For New SysAdmins

- 25 PHP Security Best Practices For Sys Admins
- The Novice Guide To Buying A Linux Laptop
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
Facebook it - Tweet it - Print it -


{ 17 comments… read them below or add one }
Hello,
the most secure thing (and the most ugly one) you can do is delete all files from EXIM input folder (/var/spool/exim/input). The folder that uses exim to keep the mail queue .
EXIM creates 2 files for email – 1 for the headers part and another for the message body. Erasing everything from this folder must erase the queue.
EXIM names the files with the msg_id an put -H or -D part on the end of filename – H for headers an -D for body file
The solution of Vivek is good, but for less traffic, otherwise if you manage lots of emails I recommend you to rm the files.
One of my cases:
[root@mta1 ~]# exim -bpcO/p:
… and i afraid i cannot use this solution.
Regards
Ol.
Bulgaria
Another solution is put this router in the EXIM configure and run some queue runners – exim -qff&
If you place the router in first place he will delete all messages older than X seconds. 1800 = 30 min.
router_mailxpire:
driver = redirect condition = ${if >= {$message_age}{1800}} data = /dev/null file_transport = address_file no_more… you are welcome :)
Cheers
Oldzhay,
Thanks for sharing your solution!
this is a much cleaner/safer way to do the same thing (using the tools provided by all modern exim installations)
exim -bp | exiqgrep -i | xargs exim -Mrm
exim -bp # this prints the contents of the mail queue
exiqgrep # extracts the id from each mail
xargs exim -Mrm # removes the mail
exiqgrep is pretty flexible (for instance if you wanted to only extract the frozen email id’s used exiqgrep -iz ), exipick is an extension of exiqgrep that can help a bit too.
Dallas,
The faq has been updated with your command. Thanks for sharing with us!
Have you been remove about 1200821 queue ?
it wont work with “exim -bp | exiqgrep -i | xargs exim -Mrm” ( I tried to wait until 24hours, it get my swap until 0 free)
so it must use script to remove the queue, in my case:
-list all directory in ‘input’ folder:
cd /var/spool/exim/input ; ls >> /tmp/folder
-create a script name “removeall”
echo “for b in `ls` ; do rm -f $b ; done” >> /tmp/removeall ; chmod 700 /tmp/removeall
- go to input folder and exec command
for a in `cat /tmp/folder` ; do cd $a ; /tmp/removeall ; cd .. ; done
and it work great :)
Thanks Oldzhay for you router to delete messages older than x seconds, I will definately test & apply in my setup.
Well, I remove all the messages in the queue but still am not able to send any mail :(
“Well, I remove all the messages in the queue but still am not able to send any mail”
…this is another problem. Can you post some log here pls, and we can look for a solution
akbar:
Your function can be simplified as:
for dir in /var/spool/exim/input/*; do cd $dir; ls | xargs rm -f; doneOf course, if your intention is to simply remove all files (not directories) underneath /var/spool/exim/input, that process can be simplified as follows:
find /var/spool/exim/input -type f -exec rm -f {} +Of course, this assumes that there are no files below input that shouldn’t be deleted.
To check the no. of mails in the queue use the command
exim -bpc
To check the list of pending mails use
exim -bp
To deliver the mails use
exim -qf -d
If working on a Server then check the load on it (using the command w ) and if the load is not high (less than 1.o) then you could also set 1 or 2 mail runners (do not set more than 2 as the load on the server may increase) using the command
exim -qf &
Hello,
About the Dallas Marlow comment:
It’s possible to do it without the “exim -bp” command.
The command “exiqgrep -i | xargs exim -Mrm” does exactly the same, because exiqgrep looks up the queue by itself.
Regards,
J. Ruiz
i want to remove 4000 messages
I client of my have been hacked and they sent about 40 millions of mails.
I have all of them in the input folder, i cant ls, du or nothing, this is a sh*t.
Anyone have some script that do this really fast ? i dont want to wait 1 month for clean up.
Another command which can be used for this is the following
exim -bp |awk {‘print $3′} |xargs exim -Mrm
Tyler,
Thanks for your post — I saw what Akbar was trying to accomplish, but was wondering if there was a simpler way to do, and then I read down further to your post.
I had a client who had over 300,000 messages in the Exim Queue after his server was compromised by a PHP script and using exiqgrep didn’t seem to work (since I imagine exiqgrep doesn’t expect to encounter that many messages in Exim’s queue in the first place).
I created a shell command out of your command there, and ran it, and voila — everything is back to normal.
In addition, I think it may be of note to mention that configserver.com makes two applications that are free for WHM called CMM (Mail Manager) and CMQ (Mail Queue Manager) which help with these kinds of issues, but of course if you have a massive queue (300,000 messages for example) they won’t really help much, since they also use Exim’s built-in toolset to run the commands.
Thanks again Tyler! =0)
Thanks Tyler. It works.!