How to remove mail from Postfix queue
Sometimes you need to remove some mail from queue or do other things in shell. For experienced administrators this is not a problem, but I decided to write some usable shell commands and scripts for less experienced users.
Here are few commands to remove mail from Postfix queue. Don’t ask me why – just for example. This time i will describe it step by step. This is mail queue:
# mailq
-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
571AAC45 48204 Tue Dec 21 07:52:39 MAILER-DAEMON
(connect to example.com[221.X.X.70]:25: Connection refused)
datadm@example.com
...
Let’s choose only messages from MAILER-DAEMON:
# mailq|grep MAILER-DAEMON
571AAC45 48204 Tue Dec 21 07:52:39 MAILER-DAEMON
5C0671838 4528 Mon Dec 20 15:19:35 MAILER-DAEMON
806BF1FB0 12469 Mon Dec 20 00:38:55 MAILER-DAEMON
2CDC81BDE 47643 Mon Dec 20 12:05:33 MAILER-DAEMON
623E21AFE 4096 Mon Dec 20 13:16:59 MAILER-DAEMON
682491647 11799 Wed Dec 22 06:18:43 MAILER-DAEMON
...
Operations are done on Queue-ID, so we need to select only first column:
# mailq|grep MAILER-DAEMON|cut -d" " -f1
571AAC45
5C0671838
806BF1FB0
2CDC81BDE
623E21AFE
682491647
...
Now, send it to postsuper
command:
# mailq|grep MAILER-DAEMON|grep ^5|cut -d" " -f1|postsuper -d -
postsuper: 571AAC45: removed
postsuper: 5C0671838: removed
postsuper: Deleted: 2 messages
I limited scope to those, which begin with 5 (grep ^5
), to show that grep
and cut
, can be replaced with one gawk
(GNU awk) command:
# mailq|gawk '/MAILER-DAEMON/ { print $1 }'|postsuper -d -
postsuper: 806BF1FB0: removed
postsuper: 2CDC81BDE: removed
postsuper: 623E21AFE: removed
postsuper: 682491647: removed
...
If it happens that the argument list is too long, then you need to use xargs
command before postsuper
.
# mailq|gawk '/MAILER-DAEMON/ { print $1 }'|xargs|postsuper -d