Clear/Delete/Flush Queued Mail In Sendmail

Delete-queued-mail
mm
Written By:- Santosh Prasad

You can permanently delete all email messages in the Sendmail Mail Server (SMTP) queue using Linux command line. To check the summary of the queued mail messages you can use the mailq or sendmail -bp command. Execute the following command.

Check Queued Mail

# mailq
OR
# sendmail -bp

You will get some output like below:

                /var/spool/mqueue ( 9 requests)
-----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------
v3K6kjED005817     2493 Thu Apr 20 01:46 MAILER-DAEMON
                 (Deferred: Connection refused by app07.example.com.)
                                         <[email protected]>
v3K6kdED005811     2493 Thu Apr 20 01:46 MAILER-DAEMON
                 (Deferred: Connection refused by app07.example.com.)
                                         <[email protected]>
v3K6AI0m007910      419 Thu Apr 20 01:10 <[email protected]>
                 (Deferred: 452-4.2.2 The email account that you tried to reac)
                                         <[email protected]>
v3K4jJC7032479     2494 Wed Apr 19 23:45 MAILER-DAEMON
                 (Deferred: Connection refused by app07.example.com.)
                                         <[email protected]>
v3K5FrUH024347       95 Thu Apr 20 00:15 <[email protected]>
                 (Deferred: 450-4.2.1 The user you are trying to contact is re)
                                         <[email protected]>
v3K4KPpR011626     2485 Wed Apr 19 23:20 MAILER-DAEMON
                 (Deferred: Connection refused by app07.example.com.)
                                         <[email protected]>
v3K2ldkT025732       92 Wed Apr 19 21:47 <[email protected]>
                 (Warning: could not send message for past 4 hours)
                                         <[email protected]>
v3K1Eivj015799       90 Wed Apr 19 20:14 <[email protected]>
                 (reply: read error from example.com)
                                         <[email protected]>
v3JLq7wH011947     2493 Wed Apr 19 16:52 MAILER-DAEMON
                 (Deferred: Connection refused by app07.example.com.)
                                         <[email protected]>
                Total requests: 9

Delete All Queued Mail

If you want to delete all queued mail just cd to /var/spool/mqueue/ and delete all files.

# cd /var/spool/mqueue/
# ls
# rm *

Or simply

# rm -rvf /var/spool/mqueue/*

Note: Please make sure you are removing all /var/spool/mqueue/ files if you did something wrong with rm -rvf with * you may damage your system.

Delete Queued Mail Using qtool.pl Tool

Sendmail mail server does not provide any command to delete messages from the mail queue. You can use qtool.pl script located in sendmail source code contrib directory as follows:

# mailq

Sample output:

                /var/spool/mqueue ( 9 requests)
-----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------
v3K6kjED005817     2493 Thu Apr 20 01:46 MAILER-DAEMON
                 (Deferred: Connection refused by app07.example.com.)
                                         <[email protected]>
v3K6kdED005811     2493 Thu Apr 20 01:46 MAILER-DAEMON
                 (Deferred: Connection refused by app07.example.com.)
                                         <[email protected]>
v3K6AI0m007910      419 Thu Apr 20 01:10 <[email protected]>
                 (Deferred: 452-4.2.2 The email account that you tried to reac)
                                         <[email protected]>
v3K4jJC7032479     2494 Wed Apr 19 23:45 MAILER-DAEMON
                 (Deferred: Connection refused by app07.example.com.)
                                         <[email protected]>
v3K5FrUH024347       95 Thu Apr 20 00:15 <[email protected]>
                 (Deferred: 450-4.2.1 The user you are trying to contact is re)
                                         <[email protected]>
v3K4KPpR011626     2485 Wed Apr 19 23:20 MAILER-DAEMON
                 (Deferred: Connection refused by app07.example.com.)
                                         <[email protected]>
v3K2ldkT025732       92 Wed Apr 19 21:47 <[email protected]>
                 (Warning: could not send message for past 4 hours)
                                         <[email protected]>
v3K1Eivj015799       90 Wed Apr 19 20:14 <[email protected]>
                 (reply: read error from example.com)
                                         <[email protected]>
v3JLq7wH011947     2493 Wed Apr 19 16:52 MAILER-DAEMON
                 (Deferred: Connection refused by app07.example.com.)
                                         <[email protected]>
                Total requests: 9

Now note down Q-ID such as v3K6kjED005817 and run the qtool.pl like below:

# ./qtool.pl -C /etc/mail/sendmail.cf -d /var/spool/mqueue/v3K6kjED005817

Where:

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

If you want to delete all queued mail for the example.com domail. You will need to set QIDS either using the mailq command or manually like below.

QIDS="qid1 qid2 qidN"

OR

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

Now follow the base for loop to delete all queued mail for example.com.

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

I hope this article will help you to flush queued mail in Sendmail. If you have any queries and problem please comment in comment section.

Thanks:)

About Author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

Other Post by Santosh Prasad

Visit All Post

Related Article

You may also Like

Leave a Comment

Shares