How to clear the Qmail Queue (clean & delete it quickly)

Awe shiznit!! Is your qmail queue on your server out of control? Look no further! Here’s a quick and easy way to clear, clean, or empty your qmail queue.

Clear & Clean Qmail Queue Completely, Safely & Fast!

This script should not remove anything it should not remove, and it should clear all e-mail from your qmail queue safely so you can start sending mail normally again. (This is for qmail only and not a solution for any other mail programs.) You’re going to need ssh access and an ssh program like putty or putty portable.

Let’s get busy with the Qmail Clear script:

  1. Log into your server through ssh
  2. type this:
    su -
  3. type this:
    wget http://www.howtohacks.com/scripts/qmailclear.sh
  4. type this:
    sh qmailclear.sh or sh ./qmailclear.sh
  5. done.

You can download it and view it yourself, but just as a quick summary so you know what it’s doing, the Qmail Clear script looks like this:

echo Cleaning Queue
mv /var/qmail/queue/lock /root/
cd /var/qmail/queue; find . -type f -exec rm -f ‘{}’ \;
mv /root/lock /var/qmail/queue/
echo Queue is now Clean

If this didn’t do the trick and you’re receiving oversized file truncating errors (or “Argument list too long”) then you should use this more intense script. It will take longer but it will get the job done.

Let’s get busy with the Qmail Clean script :

  1. Log into your server through ssh
  2. type this:
    su -
  3. type this:
    wget http://www.howtohacks.com/scripts/qmailclean.sh
  4. type this:
    sh qmailclean.sh or sh ./qmailclean.sh
  5. done.

DO NOT restart any process. DO NOT reboot the server. DO NOT do anything until the script says it is done. If you restart a process, etc, it will break qmail and that’s no fun. Just run the script until it’s complete.

You can download the script and view all of the code yourself, but here’s a quick summary of the main lines of code so you know what’s taking place when running the Qmail Clean script:

echo “Stopping Qmail”
/etc/init.d/qmail stop
echo “Clearing the Mail Queue”
cd /var/qmail/queue/info
rm -rf blah blah blah
echo “Info folder clean, clearing mess folder”
cd /var/qmail/queue/mess
rm -rf blah blah blah
echo “Mess folder clean, clearing remote folder”
cd /var/qmail/queue/remote
rm -rf blah blah blah
echo “Remote folder clean, clearing intd folder”
cd /var/qmail/queue/intd
rm -rf blah blah blah
echo “Intd folder clean, clearing local folder”
cd /var/qmail/queue/local
rm -rf blah blah blah
echo “Local folder clean, clearing todo folder”
cd /var/qmail/queue/todo
rm -rf blah blah blah
echo “Mail queue cleaned”
echo “Restarting Qmail Now”
/etc/init.d/qmail start
echo “Qmail Started!”
echo “Done!”

If you’re interested in reading more about Qmail I recommend this book Qmail book and also these links:
A reference for qmail users – qmail.org
A qmail tutorial site – Life With Qmail

13 thoughts on “How to clear the Qmail Queue (clean & delete it quickly)

  1. Hi, I wonder if some still looking after this. I have a bit of a problem. I run this command succesfully a few weeks back. It took a while but only around 20min. Now, I had to run it again, but after over 40min is still stuck at:
    “info folder clean, clearing mess folder”
    I really beleive it should take that long at all. I think someting might be wrong. Is ther any way I could know if this is running or is just stuck or if I cancel it I will break everything? I´m not really savy on SSH so I dnt know how to get out of this!! Any ideas? Pleeeaaaseeeee

    Thanx

  2. The fastest way to massive amounts of qmail messages in the queue is by combining Eric and Sum’s methods…then adding a little extra. The following method deleted 500k queues messages in ~120 seconds:

    #!/bin/bash

    QMAIL_QUEUE=/{PATH TO QMAIL}/queue

    /{PATH TO INIT SCRIPT}/qmail stop

    for foo in info mess remote intd local todo
    do
    for bar in `ls $QMAIL_QUEUE/$foo`
    do
    find $QMAIL_QUEUE/$foo/$bar -type f -print0 | xargs -0 rm -fv
    done
    done

  3. We have just under 500,000 in the queue… am running the qmailclean due to the size of the queue. it has been like 20mins… will wait and see how much longer it takes. will update when done.

  4. thanks so much for this.. my server had run out of inodes and I discovered it was qmail with tens of thousands of file handles

    quick google and stumbled on your site, and the above worked a treat

    thanks again

    t

  5. This only works if you have a small amount of mail in your queue. If you have enough e-mail to warrant the use of this script:
    ./qmailclear.sh: /bin/rm: Argument list too long

    One example : ls 15 | wc -l
    56193

    Try this (replace where necessary)

    #!/bin/bash

    QMAIL_QUEUE=/{path-to-qmail-queue}/queue

    /{path-to-qmail-init-scripts}/qmail stop

    for foo in info mess remote intd local todo
    do
    for bar in `ls $QMAIL_QUEUE/$foo`
    do
    cd $QMAIL_QUEUE/$foo/$bar
    ls | xargs rm
    done

    done

    /{path-to-qmail-init-scripts}/qmail start

  6. Anyone ever heard of find?

    cd /var/qmail/queue &&
    find info mess remote intd local todo -type f -exec rm {} \;

  7. use this instead

    for foo in info mess remote intd local todo
    do
    for bar in `ls $QMAIL_QUEUE/$foo`
    do
    find $QMAIL_QUEUE/$foo/$bar -type f -exec rm -rfv {} \;
    done

    done

  8. is there many folders in your /var/qmail/queue/mess? after I use the cleaner , I cannot use send email anymore

    ERROR:
    Message not sent. Server replied:
    Requested action aborted: error in processing
    451 qq trouble creating files in queue (#4.3.0)

    can anyone help?

  9. Thank you for the awesome utility to clear my qmail queue. I’m having a problem with a lot of spam getting through, not being rejected for some reason and it’s just building up in the queue and eventually crashing the smtp server hosted by qmail ( I think). Do you have any suggestions that I could use for my spam settings.. I’ve checked with the tech who “hold” my server and they say the spam settings I have are good.

    Any help you could provide would be awesome!

    Thanks. Mike

    1. Mike – I’m glad this helped. For the spam, I’m not sure what you can do if your qmail queue is getting bogged down. Your hosting company should help you (even though they say your settings are fine). If you’re always getting the queue filled up there must be an issue on the server with something (vague I know).

      If it’s spam being sent to you and landing in your inbox you might look at having Spam Assassin installed. Here’s a wikipedia page about it. In the options you can mark spam suspect emails with [SPAM] in the subject and filter them to the junk mail box in Outlook, etc. Or you can have Google/Gmail handle it by using their service. Good luck!

  10. May i know if these scripts will clear important emails. Say if i were to run these scripts everyday at midnight.

    Is there a possibility that these scripts will clear valid email sent while the script is running?

Leave a 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>