Quick Tip: Postfix Flush the Mail Queue

by Vivek Gite · 9 comments

Traditional "sendmail -q" command flushes mail queue. Under Postfix, just enter the following to flush the mail queue
# postfix flush
OR
# postfix -f

To see mail queue, enter:
# mailq

To remove all mail from the queue, enter:
# postsuper -d ALL

To remove all mails in the deferred queue, enter:
# postsuper -d ALL deferred

postfix-delete.pl script

Following script deletes all mail from the mailq which matches the regular expression specified as the first argument (Credit: ??? - I found it on old good newsgroup)

#!/usr/bin/perl
 
$REGEXP = shift || die "no email-adress given (regexp-style, e.g. bl.*\@yahoo.com)!";
 
@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";
};
close(POSTSUPER);
 

For example, delete all queued messages from or to the domain called fackspamdomain.com, enter:
./postfix-delete.pl fackspamdomain.com
Delete all queued messages that contain the word "xyz" in the e-mail address:
./postfix-delete.pl xyz

Updated for accuracy.

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 9 comments… read them below or add one }

1 Matt 12.10.07 at 11:49 pm

The command for “To remove all bounced mail from Queue, enter:” is incorrect. That command will completely delete all the contents of your Mail Queue.

When I read this I thought it would eliminate only the bounced back emails that were sitting in my queue. I was wrong. It deleted all 6,000 of the emails sitting in my queue.

If this post is any sign of the type of information provided on this website, the readers are in for a big surprise…and some pink slips from their employer..

2 vivek 12.11.07 at 8:45 am

Matt,

thanks for the heads up. It was a typo. The post has been updated

3 Henk 12.11.07 at 3:46 pm

I think matt deserves a pink slip from his employer, because he uses commands found on random sites without even checking what the effects are!

4 Jim 04.24.08 at 6:30 pm

I’ve found this code on two different blogs and have received compilation errors both times similar to:

Unmatched right curly bracket at ./delete-queue line 9, at end of line
(Might be a runaway multi-line ;; string starting on line 5)
syntax error at ./postfix-delete.pl line 9, near “}”
Execution of ./postfix-delete.pl aborted due to compilation errors.

I’m more of a PHP person so Perl’s not my thing. I know syntax is really important and I’ve double and triple checked to make sure things are correct, including composing in vi, pico and bbedit editors.
??

5 Manuel 05.02.08 at 1:20 am

Wow This is a seriously crappy coding job. Who writes this stuff? I cleaned this up to not only make sure this was scoped correctly, but also try to have a novice perl user understand it.

#!/usr/bin/perl

my $REGEXP = shift(@ARGV) || die “no email-adress given (regexp-style, e.g. bl.*\@yahoo.com)!”;

my %Q;
my $queue_id;
my @data = `/usr/sbin/postqueue -p`;
foreach my $line (@data) {
if ($line =~ /^(\w+)(\*|\!)?\s/) {
$queue_id = $1;
}
if($queue_id) {
if ($line =~ /$REGEXP/i) {
$Q{$queue_id} = 1;
$queue_id = “”;
}
}
}

open(POSTSUPER,”|postsuper -d -”) || die “couldn’t open postsuper” ;

foreach my $key (keys %Q) {
print POSTSUPER “$key\n”;
}
close(POSTSUPER);

Hopefully this helps.. I cannot confirm that this works, just cleaned it up so that it makes sense.

6 pd 07.25.08 at 2:08 am

is there someone help me out? i need scripts to check how many emails bounced or deferred or sent?
if bounced or deferred then why? like full details message?

7 Sean 11.07.08 at 3:27 pm

Manuel, learn to indent.

8 somono 02.11.09 at 8:08 am

If a queue_id of – is specified, the program reads queue IDs from standard input. For example, to delete all mail with exactly one sender info@msn.com :

mailq | tail +2 | grep -v '^ *(' | awk  'BEGIN { RS = "" }
              # $7=sender, $8=recipient1, $9=recipient2
               { if ($7 == "info@msn.com")
               print $1 }
              ' | tr -d '*!' | postsuper -d -
9 salman 06.25.09 at 9:43 am

thanx alot alot ………………. for the script it works very very very well …… very very very well done …. !!!!!!!!!!!!!!

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post:

Next post: