Delete Qmail Server messages Queue

qmail is a mail transfer agent that runs on Unix. It was more secure replacement for the popular Sendmail program. The author offered a $500 prize for the first person to publish a verifiable security hole in the latest version of the software.

This is a useful thing to do in a number of situations. For instance, if you are hit with a spamming attack, you can temporarily instate a second Qmail installation (once the spam run is finished), allow it to take over mail receipt,and then use this tool to clean the offending mails out of the queue before switching over to the main Qmail installation once again.

Occasionally, viruses will get past scanners before the signatures get updated; if they exist in large numbers, it is often practical to stop the Qmail install briefly in order to clean out all messages containing a signature related to the virus.

Whatever the reason to pull items from your mail queue, this program will delete them in such a manner that will let you restore them easily.

Here we are going to see different Technics how to delete or remove your qmail server queue

Qmail-Remove

This is definitely my first choice.Qmail-Remove will remove messages containing a particular string from your Qmail queue.

Mails are *not* deleted from the queue! They are only stored, temporarily, in $qmail-queue/yanked/, where you can view
them individually and restore them back to the queue manually. There is currently no support for restoring them automatically.

By default, Qmail-Remove assumes that your Qmail queue is stored in /var/qmail/queue, but this can be changed with a
command line option. Similarly, Qmail-Remove assumes that your queue “split” is 23 by default, among other things.

If you want to check your qmail queue using the following command

# /var/qmail/bin/qmail-qstat

Output looks like

messages in queue: 567154
messages in queue but not yet preprocessed: 3

Install Qmail-Remove

First you need to download latest version from here current version is Qmail-Remove 0.95

Download using the following command

#wget http://www.linuxmagic.com/opensource/qmail/qmail-remove/qmail-remove-0.95.tar.gz

Now you have qmail-remove-0.95.tar.gz file and now you need to extract using the following command

#tar -zxvf qmail-remove-0.95.tar.gz

Now you should have qmail-remove-0.95 folder go in to the directory and run the following commands

#make

#make install

This will complete the installation.

Now you need to create a directory named “yanked” in the qmail queue directory you intend to use before using this program.

#mkdir /var/qmail/queue/yanked

Using qmail-remove

Syntax

qmail-remove [options]

Available options

-e use extended POSIX regular expressions

-h, -? this help message

-i search case insensitively [default: case sensitive]

-n limit our search to the first bytes of each file

-p specify the pattern to search for

-q specify the base qmail queue dir [default: /var/qmail/queue]

-r actually remove files, without this we’ll only print them

-s specify your conf-split value if non-standard [default: 23]

-v increase verbosity (can be used more than once)

-y directory to put files yanked from the queue [default: /yanked]

-X modify timestamp on matching files, to make qmail expire mail is the number of seconds we want to move the file into
the past.specifying a value of 0 causes this to default to (604800)

-x modify timestamp on matching files, to make qmail expire mail is a date/time string in the format of output of the
“date” program.

Examples for qmail-remove

Before doing any thing related to qmail queue you need to stop the qmail service using the following command

#/etc/init.d/qmail stop

To delete mails from Que,

#qmail-remove -r -p

# qmail-remove -r -p gtre.ac.net

324001: yes
moved mess/0/324001 to yanked/324001.mess
moved remote/0/324001 to yanked/324001.remote
moved info/0/324001 to yanked/324001.info
324024: yes
moved mess/0/324024 to yanked/324024.mess
moved remote/0/324024 to yanked/324024.remote
moved info/0/324024 to yanked/324024.info

This will remove all emails in que with “gtre.ac.net” in it and place it in /var/qmail/queue/yanked folder.

qmhandle

qmHandle is a simple program which allows you to view and manage the qmail message queue.

qmHandle Advantages

You can read the qmail queue, like you do with the qmail-qread program.

However, the output of this program is improved over qmail-qread, with the output of the message subjects and color capabilities;

You can print queue statistics, like qmail-qstat, with color capabilities;

You can view a message in the queue;

You can remove one or more messages from the queue;

Written in Perl, and therefore easily customizable and truly multiplatform.

Install qmhandle

First you need to download latest version from here and the current version is qmhandle-1.2.0

Download using the following command

#wget http://mesh.dl.sourceforge.net/sourceforge/qmhandle/qmhandle-1.2.0.tar.gz

Now you have qmhandle-1.2.0.tar.gz file you need to extract using the following command

#tar xzvf qmhandle-1.2.0.tar.gz

You should be having qmhandle-1.2.0 folder go in to the folder and you need to edit the qmHandle file to configure the
following settings

my ($queue) = ‘/var/qmail/queue/’;

my ($stopqmail) = ‘/etc/init.d/qmail stop’;

my ($startqmail) = “/etc/init.d/qmail start”;

my ($pidcmd) = ‘pidof qmail-send’;

Using qmHandle

Available options for qmHandle

Available parameters are:
-a : try to send all queued messages now (qmail must be running)

-l : list message queues

-L : list local message queue

-R : list remote message queue

-s : show some statistics

-vN : display message number N

-dN : delete message number N

-Stext : delete all messages that have/contain text as Subject

-D : delete all messages in the queue (local & remote)

-V : print program version

Additional (optional) parameters are:

-c : display colored output

-N : list message numbers only (to be used either with -l, -L or -R)

Examples

list message queues

#qmHandle -l

delete all messages that have/contain text as Subject

#qmHandle -l -S yahoo.com.tw

Other Solutions

1)You can edit the /var/qmail/control/queuelifetime file this is the file to control how long a message stays in a queue.Just put a number (to represent seconds)in this file.By default 86400 sec Will keep the mail for 1 day and expire after that.Here you can change this value to 1 and restart your qmail server it should clear your qmail queue.

2)Deleting mails from qmail queue

Following commands can delete all mails from your qmail mail server queue.

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

Sponsored Link

14 thoughts on “Delete Qmail Server messages Queue

  1. Hi, I have built 2 simple scripts to do the cleaning job based on the emails content to remove (actually for solaris):

    /var/qmail/bin/removemsg.sh
    —————————-
    #!/bin/sh

    DATA=`/usr/bin/date ‘+%Y%m%d-%H%M%S’`

    if [ -f /var/qmail/queue/mess/$1 ]; then
    mv /var/qmail/queue/mess/$1 /var/qmail/queue/removed/$1-$DATA
    echo moved /var/qmail/queue/mess/$1 to /var/qmail/queue/removed/$1-$DATA >> /var/qmail/queue/rm.log
    fi
    if [ -f /var/qmail/queue/info/$1 ]; then
    rm /var/qmail/queue/info/$1
    echo removed /var/qmail/queue/info/$1 >> /var/qmail/queue/rm.log
    fi
    if [ -f /var/qmail/queue/local/$1 ]; then
    rm /var/qmail/queue/local/$1
    echo removed /var/qmail/queue/local/$1 >> /var/qmail/queue/rm.log
    fi
    —————————-

    and the script You’ll use do start the job. ( the example below to remove all emails containing “661HW” in the body.

    removewithcontent.sh
    —————————-
    #!/usr/bin/tcsh
    cd /home/qmail/queue/mess

    find . -exec grep 661HW \{\} \; -exec /var/qmail/bin/removemsg.sh \{\} \; > /tmp/661HW.queue &
    —————————-

  2. All very neat, but why run a mail server that generates a ton of back scatter SPAM. The email world would be helped a lot more by migrating all qmail users to a mail server that rejects garbage at the SMTP level instead of accepting everything, then spraying bounce messages to innocent users. I like Exim, others may prefer Sendmail or Postfix, but please don’t use qmail. Pretty please.

  3. Dear Neil,
    fighting spam and cleaning queues are two subjects totally different.
    I like qmail because il like LEGO, you can add, replace, customize every component.
    Incoming email are never black or white, how do You recognize ?
    – spam filters (spamassasin)
    – dns-bl that filer remote IPs
    – greylisting
    all can be done with a lot of enhancements in qmail, the question is which one is the good for your situation. All MTA are equivalent.

    Our domains have been used twice as sender domain from chinese spammers, and there is nothing you can do than clearing the queue. No filters can block tons of bounching emails because they don’t come from bad IP and they don’t contains bad words.

  4. 3000 mails are in queue.And it deliver the mail very slow. We can’t delet the mail queue. How the way we will solve it.

    Regards
    Prasenjit

  5. THANK YOU!!! THANKS HEAPS!!! THANKS AGAIN!!!

    I removed a directory and didn’t realize there was a cron job running on it. As soon as I deleted the directory, error messages (first a ‘cd’ then a ‘php cron.php’) started piling up in my queue. Running * * * * * emails piled up each day – quickly surpassing the GoDaddy limit and loading up the queues.

    GoDaddy doesn’t load qmail-remove on their servers, now I know!!

    Thanks again!
    oh4real

  6. Thanks a lot for the info, it really did help me out! I first installed qmail-remove, but qmHandle does it all (listing, removing, etc.) and in a very userfriendly way. I appreciate the good work!

  7. hello
    how can install qmail-remove without compile ?
    In a server i haven’t possibility to install any Compiler

    tnks

  8. Amazing! Saved me big time. I don’t know much about unix so I didn’t realize you need to run it as ./qmHandle

    Once I figured that out it worked like a charm.

  9. This worked for me 🙂
    ===============
    [root@machine]# /var/qmail/bin/qmail-qstat
    messages in queue: 14362
    messages in queue but not yet preprocessed: 42
    [root@machine]# qmhandle.pl -D
    [root@machine]# /var/qmail/bin/qmail-qstat
    messages in queue: 0
    messages in queue but not yet preprocessed: 0
    [root@machine]#
    ===============

  10. this program is a good idear! may be also an idear to put ‘gcc ‘ into the archive, because
    i’ve recived an error, that gcc was not found on my server …

  11. Please help…. I am VERY new to this stuff. I successfully downloaded the application and unpacked it, but when I typed the “make” and “make install” commands, all I got was:

    [root@mydomain ~]# make

    make: *** No targets specified and no makefile found. Stop.

    [root@mydomain ~]# make install

    make: *** No rule to make target `install’. Stop.

    I am a TOTAL linux n00b, so please be gentle. LOL

Leave a comment

Your email address will not be published. Required fields are marked *