Troubleshooting Delivery Issues

Fastest Way to Completely Clear the Mail Queue in cPanel (Exim)

Published · Updated 3 min read
Knowledge base article
Contents (11 sections)

When your cPanel server has an excessively large mail queue due to abuse or spam, clearing it quickly is crucial. This guide provides the fastest and most efficient methods to clear the Exim mail queue and prevent future abuse.


Step 1: Check the Mail Queue Size

Before clearing the queue, determine how many emails are queued:

code
exim -bpc

This command displays the total number of emails in the queue.

To list the queued emails:

code
exim -bp

To find the top senders contributing to the queue:

code
exim -bp | awk '{print $3}' | sort | uniq -c | sort -nr | head -10

Step 2: Clear the Mail Queue

To remove all emails from the queue:

code
exiqgrep -i | xargs exim -Mrm

This efficiently clears the entire queue.

If the above command fails due to malformed message IDs, use:

code
for i in $(exiqgrep -i); do exim -Mrm $i; done

This loops through each message ID and removes them individually.

Method 2: Force-Delete Mail Queue Files

Stop Exim before clearing the queue:

code
systemctl stop exim

Then, delete all queued emails:

code
rm -rf /var/spool/exim/input/* /var/spool/exim/msglog/*

Restart Exim:

code
systemctl start exim

If errors like "Directory not empty" occur, use:

code
find /var/spool/exim/input -type f -exec rm -f {} +
find /var/spool/exim/msglog -type f -exec rm -f {} +
find /var/spool/exim/input -mindepth 1 -type d -empty -delete
find /var/spool/exim/msglog -mindepth 1 -type d -empty -delete

If Exim's queue is still not clearing, rename and recreate the directory:

code
mv /var/spool/exim/input /var/spool/exim/input.bak
mkdir -p /var/spool/exim/input
chmod 750 /var/spool/exim/input
chown -R mail:mail /var/spool/exim/input
rm -rf /var/spool/exim/input.bak

Step 3: Restart Exim and Verify the Queue

After clearing, restart Exim:

code
systemctl start exim

Check if the queue is empty:

code
exim -bp

If no output appears, the mail queue has been successfully cleared!


Step 4: Prevent Future Spam and Abuse

To avoid repeated abuse, take the following measures:

1. Limit Outgoing Emails Per Hour

code
whmapi1 setacctpkg pkgname=default maxemailsperhour=100

This restricts bulk email sending.

2. Scan for Malicious Scripts

code
grep -Ri "mail(" /home/<username>/public_html/
maldet -a /home/<username>/public_html/

Detects unauthorized email-sending scripts.

3. Enable SMTP Restrictions in WHM

Navigate to WHM > Tweak Settings > SMTP Restrictions and enable it to prevent unauthorized scripts from sending mail.

4. Monitor Email Sending in Logs

To identify compromised email accounts:

code
grep "A=dovecot_login" /var/log/exim_mainlog | awk '{print $5}' | sort | uniq -c | sort -nr | head -20

To check for excessive script-based email sending:

code
grep "cwd=" /var/log/exim_mainlog | awk -F"cwd=" '{print $2}' | cut -d " " -f1 | sort | uniq -c | sort -nr | head -10

Conclusion

Clearing a massive email queue in cPanel's Exim can be done quickly using these methods. By using the fastest commands, force-deleting the queue, and implementing security measures, you can prevent further spam-related issues and keep your server stable.

Regular monitoring, strict email policies, and proactive security scans will help prevent future mail queue overloads!

Was this article helpful?

Your answer helps us decide what to improve next.

Still need help? Open a support ticket and our team will reply.

Prefer an app? Add this site to your home screen.Get the app