Sendmail: Clear / Delete / Flush Mail Queue

by on July 1, 2011 · 9 comments· LAST UPDATED July 1, 2011

in

How can I permanently delete all e-mail messages in the Sendmail mail server (SMTP) queue using UNIX / Linux command prompt?

You can use the mailq command sendmail -bp command to display a summary of the mail messages queued for future delivery. Type the following command:
# mailq
OR
# sendmail -bp
Sample outputs:

		/var/spool/mqueue (1 request)
-----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------
p61J75u5037681      893 Fri Jul  1 14:07 
      8BITMIME   (Deferred: Connection timed out with example.com.)
					 
		Total requests: 1

You can cd to /var/spool/mqueue and delete all files if you want to get rid of all messages in the queue:
# cd /var/spool/mqueue/
# ls
# rm *

qtool.pl Tool

Sendmail mail server does not provide any command to remove messages from the mail queue. The above will delete all messages from the mail queue. You can use qtool.pl script located in sendmail source code contrib directory as follows:
# mailq
Sample outputs:

		/var/spool/mqueue (1 request)
-----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------
p61J75u5037681      893 Fri Jul  1 14:07 
      8BITMIME   (Deferred: Connection timed out with example.com.)
					 
		Total requests: 1

Note down Q-ID p61J75u5037681 and run qtool.pl as follows:
# ./qtool.pl -C /etc/mail/sendmail.cf -d /var/spool/mqueue/p61J75u5037681
Where,

  • -C /etc/mail/sendmail.cf - Specify sendmail config file.
  • -d /var/spool/mqueue/p61J75u5037681 - Delete the messages specified by source. In this case by Q-ID.

To delete all messages queued for the example.com domain. First, you need to set QIDS either using the mailq command or manually as follows:

 
QIDS="qid1 qid2 qidN"
 

OR

 
QIDS="$(mailq | grep -B1 'example.com' | grep '^[a-z]'  | awk  '{print $1}' | sed  's/\*$//')"
 

Use the following bash for loop to delete all messages for example.com:

 
for q in $QIDS
do
  /usr/local/bin/qtool.pl -C /etc/mail/sendmail.cf -d /var/spool/mqueue/$q
done
 

More links and information:

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 9 comments… read them below or add one }

1 praveen July 12, 2011 at 11:36 am

Interviewer asked me one question:
User is unable to send/receive the mail.
How u will troubleshoot it?
Explain in clear manner with steps.
I didnt answer to this question.
Can u plz tell the answer to this question as soon as poss

Reply

2 Yogesh July 12, 2011 at 12:11 pm

Hello Praveen,

Your query is not answerable in one line. It requires a series of test to be performed before reaching to any conclusion.

User is unable to send/receive the mail –> Is he using mail client or web interface?
What is the error in mail client or web interface?
Is it only delay in mail or proper error mesg like 5.5.1?
Do you see any mail queue or bounce back mails?
Is your mail server blacklisted?
Is the problem while sending / receiving mails from a particular domain say hotmail.com?

To troubleshoot –> depends on Question 1

Explain in clear manner with steps –> Not possible, until you are a mail server genius :-)

Reply

3 praveen July 12, 2011 at 2:59 pm

user is using mail client. but he is unable to send/receive the mail. at server this user is added. what might be the problem. When he assiging the port of 25. it is automatically taking 25 as 543 port. And prompting the username already exits.
What to do?

Reply

4 Yogesh July 12, 2011 at 3:50 pm

Try telnet 25
This is to test firewall at client’s end
Next, observe the error mesg thrown by mail client. It helps alot.
I suppose you mistyped port 587. Both port 25 and 587 are used for outgoing SMTP. In most cases ISPs block port 25, so clients are advised to configure mail client program to point to port 587.
The sending action will ask for username / password if the check box ‘My outgoing server requires authentication’ is selected. Use ‘Use same settings as my incoming server’ to avoid this prompt.

Reply

5 amrik September 11, 2012 at 2:18 pm

In Ubuntu, the path for mail was /var/spool/mail. On deleting the files in this folder, mailq still listed some pending mails.
On some digging I found some deferred mails sitting in the /var/spool/postfix/defer and /var/spool/postfix/deferred folders. I deleted these files and my mailq is now empty.

Reply

6 Rajgopal H.G. April 2, 2013 at 7:00 am

@amrik : For postfix servers, “postsuper” is the powerful command to monitor.
Example to delete all the pending ques (Delete the ques ONLY when you are sure):
# postsuper -d ALL

Reply

7 David Ramirez May 23, 2013 at 7:37 pm

Worked like charm – even the last set of instructions pertaining to feeding the QIDS was appropriate, as some failing command flooded the queue with mails to “root@example.com”. I installed the qtool.pl script and… bye bye bad messages.

Reply

8 Daniel Moreno July 2, 2013 at 12:40 pm

In another web site (No remember) I see to flush sendmail queue:

sendmail -q -v

Reply

9 amir August 11, 2013 at 1:40 pm

find /var/spool/mqueue/* -type f -exec grep -irl “domainname.com” {} \; -exec rm -vf {} \;

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , ,

Previous Faq:

Next Faq: