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:078BITMIME (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:078BITMIME (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:
- qtool.pl from Sendmail source code contrib directory.
- Deleting mail from the mail queue using various Perl script wrapper.
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop













{ 6 comments… read them below or add one }
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
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 :-)
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?
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.
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.
@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