Sendmail: Clear / Delete / Flush Mail Queue

Posted on in Categories last updated July 1, 2011

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
# 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
		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 * 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 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
		Total requests: 1

Note down Q-ID p61J75u5037681 and run as follows:
# ./ -C /etc/mail/ -d /var/spool/mqueue/p61J75u5037681

  • -C /etc/mail/ – 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 domain. First, you need to set QIDS either using the mailq command or manually as follows:

QIDS="qid1 qid2 qidN"


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

Use the following bash for loop to delete all messages for

for q in $QIDS
  /usr/local/bin/ -C /etc/mail/ -d /var/spool/mqueue/$q

More links and information:

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Share this on (or read 10 comments/add one below):

10 comment

  1. 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

    1. 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

      To troubleshoot –> depends on Question 1

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

  2. 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?

    1. 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.

  3. 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.

    1. @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

  4. in linux mail server there is 7 mails in queue ..why get stuck mails in queues in server why not in client mail app..if there is mail id problem..what will happen if i remove queue mails form server..

Leave a Comment