Loading...

Knowledge Base

VPS or Dedicated: How to Clear the Mail Queue

If your emails are stuck, the mail queue on your VPS or dedicated server may be backed up. The mail queue holds messages that your server has accepted but hasn't delivered yet. A queue can grow when messages can't be delivered, such as mail sent to wrong addresses, or when a script or compromised account sends a large amount of mail. This article shows you how to review the queue and clear it in WHM or through SSH.

Here are the topics discussed in this article.

What You Need

  • Root access to your server. To use SSH, you also need the root password.
  • For WHM, the server's WHM login. For SSH, an SSH client, such as Terminal on Mac or Linux or PuTTY on Windows.
  • Your server's IP address or a domain name that points to your server.

Check the Queue First

Before you delete anything, find out how big the queue is and what is in it. This helps you decide whether to clear the whole queue or only part of it, and it gives you clues about why the queue grew.

Ways to clear the mail queue
What You Want to Do WHM SSH
Review the queue Mail Queue Manager exim -bpc shows how many messages are queued
Delete selected messages Delete Selected exiqgrep with a filter, such as the sender
Delete frozen messages only Select the messages with a Frozen status, then click Delete Selected exiqgrep -iz
Delete everything Delete All exiqgrep -ix and exiqgrep -iz, run one after the other

Frozen messages are messages that Exim has stopped trying to deliver. They stay in the queue until you unfreeze or delete them, or until a timeout passes.

Clear the Queue in WHM

The Mail Queue Manager in WHM lets you see the messages and delete them without typing commands. To clear the queue in WHM:

  1. Log in to WHM as root.
  2. In the search box, type Mail Queue Manager, and then click it. You can also find it under Home » Email » Mail Queue Manager.

    WHM - Mail Queue Manager search

  3. Click Run Report to list the messages. You don't need to change anything else to see the whole queue. The page shows how many messages matched, such as "The search matches 3 records."
    • The list only includes messages received between the Start Date and End Date. Check that the range covers the messages you want to see.
    • To narrow the list, type a term in the Search box, choose what to search in the Select Query menu (Search Sender, Search Recipients, Search Message ID, or No Filter), and choose a Search Type (Begins With, Exact, or Partial). Then click Run Report again.

    WHM - Mail Queue Manager

  4. Review the list. Each message shows when it was received, the sender, the message ID, the recipients, its size, and its status, such as Queued or Frozen.
  5. Delete the messages with the buttons above the list:
    • To delete only some messages, select the check box beside each one, and then click Delete Selected. To delete only frozen messages, select the ones that show Frozen in the Status column.
    • To delete every message in the list, click Delete All.
  6. If you clicked Delete All, a message asks, "Are you sure you wish to purge the entire mail queue?" Click OK to continue, or Cancel to stop. If you clicked Delete Selected, the removal starts right away without this message.
  7. The Removing All Messages in Queue window (Removing Messages in Queue if you used Delete Selected) shows each message as it is removed. When the process finishes, a message says "Process Complete." Click OK. The page then lists each removed message ID, followed by "has been removed."

Clear the Queue Through SSH

If the queue is very large, the commands in this section are often quicker than the Mail Queue Manager. First, log in to your server as root over SSH. For the steps, see How do I Get and Use SSH Access?

Count the Messages in the Queue

Run this command to see how many messages are queued:

exim -bpc

The command prints a number. Make a note of it so you can compare it after you clear the queue.

Look at the Queue More Closely

These commands help you decide what to delete. Run this command to list every queued message:

exim -bp

Each message takes two lines. The first line shows how long the message has been queued, its size, its message ID, and the sender. The second line shows the recipient:

 0m   362 1xVa9w-00000001JU4-0f6N <root@localhost>
          [email protected]

To see where the queued mail is going, send the list to exiqsumm. It groups the messages by recipient domain and shows how many there are for each:

exim -bp | exiqsumm
Count  Volume  Oldest  Newest  Domain
-----  ------  ------  ------  ------

    3    1086      0m      0m  example.invalid
---------------------------------------------------------------
    3    1086      0m      0m  TOTAL

To look at one message, replace message-id with an ID from the list. The command shows the message's header information, such as the Received line that says who submitted it, the sender, the recipient, and the subject:

exim -Mvh message-id

To follow what happened to one message, search the Exim log for its ID. The log shows when the message arrived, whether delivery failed, and whether it was removed from the queue:

grep message-id /var/log/exim_mainlog

Choose Which Messages to Delete

The exiqgrep command has options that choose which messages it lists. You can combine them to match only the messages you want. These are the most useful ones:

Common exiqgrep options
Option What It Does Example
-f Matches the sender's address. exiqgrep -f [email protected] -i
-r Matches a recipient's address. exiqgrep -r [email protected] -i
-o Matches messages older than the number of seconds you give. exiqgrep -o 86400 -i
-y Matches messages younger than the number of seconds you give. exiqgrep -y 3600 -i
-z Matches frozen messages only. exiqgrep -z -i
-x Matches non-frozen messages only. exiqgrep -x -i
-i Prints only the message IDs. This is the list that exim -Mrm deletes. exiqgrep -x -i
-c Prints the number of matching messages, such as "3 matches out of 3 messages." exiqgrep -z -c
-b Prints a one-line summary of each message with its ID, sender, and recipient, so you can review it. exiqgrep -z -b

Delete All Messages

The exiqgrep command needs at least one filter, such as -x or -z, to list messages. The queue holds two kinds of messages: non-frozen and frozen. To delete every message, run one command for each kind. First, delete the non-frozen messages:

exiqgrep -ix | xargs exim -Mrm

Then delete the frozen messages:

exiqgrep -iz | xargs exim -Mrm

Each command prints a line for every message it removes:

Message 1xVZsE-00000001JGg-1lOo has been removed

Delete Only Frozen Messages

To keep the messages that Exim is still trying to deliver and delete only the frozen ones, run this command instead:

exiqgrep -iz | xargs exim -Mrm

Delete Messages From One Sender

If one email account is responsible for the queue, you can delete only its messages. Replace [email protected] with the sender's email address:

exiqgrep -f [email protected] -i | xargs exim -Mrm

Delete One Message

To delete a single message, replace message-id with its ID:

exim -Mrm message-id

The command prints a confirmation. If the message is already gone, it says the message "has been removed or did not exist."

Confirm the Queue Is Clear

Run the count command again:

exim -bpc

If the queue is empty, the command prints:

0

If a number remains, some messages were locked while they were being sent. Wait a few minutes, and then run the delete command again.

Practice With Test Messages

If you want to try the commands before you use them on a real queue, you can add test messages to the queue yourself. Do this only on a server where you can safely delete them. As root, run:

for i in 1 2 3; do printf "Subject: queue test $i\n\nTest message $i\n" | exim -odq -f root@localhost [email protected]; done

The -odq option tells Exim to put each message in the queue without trying to deliver it. The recipient addresses end in example.invalid, a domain that can't exist, so nobody receives the messages. Run the count command to check that they are queued:

exim -bpc

The command prints the number of queued messages:

3

Exim tries to deliver queued messages every few minutes. When it can't deliver a test message, it removes the message and sends a bounce message to the sender. The bounce appears in the queue as a new message, and it may be frozen. Use the commands in this section soon after you add the test messages. You can then list them with exiqgrep -x -i and delete them with exiqgrep -x -i | xargs exim -Mrm. To remove any frozen bounce messages, use exiqgrep -z -i | xargs exim -Mrm.

Keep the Queue From Filling Again

Clearing the queue treats the symptom. If you don't fix the cause, the queue can fill up again.

  • Look at the senders. In the Mail Queue Manager, check whether most messages come from one account or script. If one account is sending mail you don't recognize, change its password.
  • Look for undeliverable mail. Your server can't deliver mail that is sent to incorrect addresses, so Exim keeps trying for a while and then gives up. When Exim gives up on a message, it removes it and sends a bounce message to the sender. A bounce that can't be delivered may stay in the queue as a frozen message until you delete it.
  • Check the mail log. If delivery keeps failing, the Exim log at /var/log/exim_mainlog shows what happened to each message.

Reset the Mail Statistics Database (Optional)

Your server keeps mail statistics in a separate database. WHM uses it for reports such as Mail Delivery Reports. Clearing the mail queue doesn't change this database, and you don't need to reset it to clear the queue. Reset it only if you want to rebuild your mail statistics, for example because the reports show incorrect data.

First, log in to your server as root over SSH. Then, to reset the database:

  1. Stop the services that update the database:
    /usr/local/cpanel/scripts/restartsrv_tailwatchd --stop
    /usr/local/cpanel/scripts/restartsrv_eximstats --stop

    You may see messages about tailwatchd when you stop and start eximstats. This is normal.

  2. Rename the old database files. The command adds a timestamp to each name, so you keep a copy:
    find /var/cpanel -name 'eximstats_db*' -exec mv -v {}{,.$(date +%s)} \;

    The command prints each file it renames:

    renamed '/var/cpanel/eximstats_db.sqlite3' -> '/var/cpanel/eximstats_db.sqlite3.1795532458'
  3. Create a new database, and then import the data from the Exim log:
    /usr/local/cpanel/bin/updateeximstats
    /usr/local/cpanel/scripts/import_exim_data /var/log/exim_mainlog

    The import may print warnings that start with "Use of uninitialized value." When it finishes, it prints:

    Data has been imported to the database from the exim log.
  4. Start the services again:
    /usr/local/cpanel/scripts/restartsrv_eximstats
    /usr/local/cpanel/scripts/restartsrv_tailwatchd --start
  5. Check that the new database exists next to the renamed copy:
    ls -lh /var/cpanel/eximstats_db*

    The list shows eximstats_db.sqlite3, which is the new database, and a file with the same name followed by a number, which is your old database.

  6. In WHM, go to Home » Email » Mail Delivery Reports, and run a report to check that your statistics appear.

Frequently Asked Questions (FAQs)

Q: What is the mail queue?
A: It is the list of messages that your server has accepted but hasn't delivered yet. Messages leave the queue when they are delivered or deleted.
Q: Can I get deleted messages back?
A: No. Deleted messages can't be recovered through WHM or Exim. Check the queue before you delete anything, and delete only what you need to.
Q: Does clearing the mail queue delete my mail statistics?
A: No. The queue and the mail statistics database are separate. Clearing the queue removes the waiting messages only. To rebuild the statistics, see Reset the Mail Statistics Database (Optional).
Q: What are frozen messages?
A: They are messages that Exim has stopped trying to deliver. They aren't tried again until you unfreeze them or a timeout passes. You can delete only the frozen messages if you want to keep the rest of the queue.
Q: Why are some messages still in the queue after I deleted them?
A: A message that is being sent at that moment is locked, so it may not be deleted. Wait a few minutes and run the deletion again.
Q: Why does the queue keep filling up?
A: Common causes are mail sent to incorrect addresses and a script or account that sends a large amount of mail. Check the senders in the Mail Queue Manager and the log at /var/log/exim_mainlog to find the cause.
How can we help you? Please contact us via phone or chat if you need assistance.

Loading...