How to Clear/Reset a Postfix Queue

Solution 1:

You should use the postsuper command to delete messages from the Postfix queue - The -d <queueid> option would delete the message with the specified queue ID.

I'm using something like this script to run through all the messages in the mail queue:

mailq | awk '$7~/@/{print$1}' | while read qid; do postsuper -d $qid; done

Solution 2:

Since you've gone an messed with the queue's files directly, you may want to issue a postsuper -p.

-p Purge old temporary files that are left over after system or software crashes.


Years ago a wrote a small script to better handle some cases in postfix mailqueues, rather than just doing ad hoc one liners. You might find it useful in the future.

$ cat /usr/local/sbin/postclear

#!/bin/bash

usage() {
        echo "${0/*\/} --from <address1> [<address2> .. <addressN>]"
        echo "${0/*\/} --bounce <address1> [<address2> .. <addressN>]"
        echo "${0/*\/} --to <address1> [<address2> .. <addressN>]"
}

if [ $# -lt 2 ]; then
        usage 1>&2 ;
        exit 1;
fi


case $1 in
        --from )
                shift
                while (( $# )) ; do
                        postqueue -p | grep -e "$1" | grep -Eo '^[A-F0-9]+' | postsuper -d -
                        shift
                done
                exit;;
        --bounce )
                shift
                while (( $# )) ; do
                        postqueue -p | grep -E -B2 -e "$1" | grep MAILER-DAEMON | grep -Eo '^[A-F0-9]+' | postsuper -d -
                        shift
                done
                exit;;
        --to )
                shift
                while (( $# )) ; do
                        postqueue -p | grep -E -B2 -e "$1" | grep -Eo '^[A-F0-9]+' | postsuper -d -
                        shift
                done
                exit;;
        * )
                echo "Unknown option $1" >&2
                exit 1;;
esac