Wednesday, April 30, 2008

Removing messages from postfix mail queue

Mail is backing up in the mail queue on my postfix server. I've found that many messages are ones I can remove, but I don't want to manually delete them one at a time. I found a posting on howtoforge that helped:

However, the command didn't work, as I think it left the -n out of the tail command. So I used this command and it worked:

mailq | tail -n +2 | awk 'BEGIN { RS = "" } / falko@example\.com$/ { print $1 }' | tr -d '*!' | postsuper -d -

Unfortunately, this didn't work right away. The mailq command was reporting nothing in the mail queue. I discovered that /usr/bin/mailq was pointing at the sendmail version of mailq. I fixed this by fixing the symlink for mta-mailq:

cd /etc/alternatives
rm mta-mailq
ln -s /usr/bin/mailq.postfix mta-mailq

Now running mailq gives the results that postfix -p does. I'm unclear on if the output of mailq is the same as postfix -p (and I didn't take the time to look), but it works this way.

I found the following command useful to see how many messages are currently in the queue:

postqueue -p | tail -1

Lastly, for some reason I couldn't seem to get the above command to remove mail from MAILER-DAEMON. Again, I didn't really look into the reason too much. But I found another solution on a howtoforge forum posting. The following deletes all mail in the queue that is from MAILER-DAEMON:

mailq | tail -n +2 | awk 'BEGIN { RS = "" } { if ($7 == "MAILER-DAEMON" ) print $1 }' | tr -d '*!' | postsuper -d -

Also, I found out that I should NOT have issued the following command to flush all queued mail. It takes all the deferred mail and makes them active again, trying to resend them. Most of those messages are in there because they couldn't connect to deliver for some reason, so retrying them all will take lots of time. It took well over a day to get through the entire queue.

postqueue -f (DON'T RUN THIS without knowing what it does)