Sendmail: Clear / Delete / Flush Mail Queue

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:

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.