There can be situations when we will need to clear our self-managed server's mail queue manually. For example, our server has been used to send a large number of emails. Some of them can freeze in the mail queue for different reasons, and we will need to clear the mail queue after stopping the sending process.
Below are the commands for viewing and clearing the mail queue in the most commonly used mail services.
Sendmail
To view the mail queue in the Sendmail service, you must first generate a queue listing file. You can do this by running one of the following commands:
# mailq# sendmail -bp
Output example:
/var/spool/mqueue (1 request) -----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------
p61J75u5037681 893 Sat Jul 2 16:00 8BITMIME (Deferred: Connection timed out with example.com.)
Go to the /var/spool/mqueue directory and delete all files:
# cd /var/spool/mqueue# ls# rm *
PostFix
To view the mail queue in the Postfix service, use the following command:
# postqueue -p
To clear the whole mail queue:
# postsuper -d ALL
To clear the frozen emails from the queue:
# postsuper -d ALL deferred
To remove a specific email from the queue, you must first find its ID. You can do this by running the following command:
# postqueue -p | grep "required_mailbox"
The output looks like:
# 056CB129DD0 5519 Mon Feb 9 09:29:19 required_mailbox
Now you can remove the specific email from the queue:
# postsuper -d 056CB129DD0
EXIM
To check the mail queue:
# exim -bp
To remove the specific email:
# exim -Mrm IDnumber
To clear the entire mail queue:
# exim -bp | awk '/^ *[0-9]+[mhd]/{print "exim -Mrm " $3}' | bash