Postfix - Delete Specific Messages from the Mail Queue

By Jack Szwergold • September 17, 2015

A nice Perl script to flush the mail queue.

A nice Perl script that deletes messages from the mail queue that matches a regular expression passed to the script as the first argument.

To create the script open up a text file named


And copy the script into that file:


$REGEXP = shift || die "no email-address given (regexp-style, e.g. bl.*\!";

@data = qx</usr/sbin/postqueue -p>;
for (@data) {
  if (/^(\w+)(\*|\!)?\s/) {
     $queue_id = $1;
  if($queue_id) {
    if (/$REGEXP/i) {
      $Q{$queue_id} = 1;
      $queue_id = "";

#open(POSTSUPER,"|cat") || die "couldn't open postsuper" ;
open(POSTSUPER,"|postsuper -d -") || die "couldn't open postsuper" ;

foreach (keys %Q) {
  print POSTSUPER "$_\n";

Now save the file and set the permissions to something readable and executable by you like this:

chmod 700

Some usage examples of the script.

For example, to delete all queued messages from or to the domain, enter:


And to delete all queued messages that contain the characters xyz in the email address:

./ xyz