Postfix Flush the Mail Queue

Traditionally you use the “sendmail -q” command to flush mail queue under Sendmail MTA. Under Postfix MTA, just enter the following command 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.

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
62 comments… add one
  • AgreeIn2017 Mar 4, 2017 @ 17:39

    Hi, still-relevant post from 2007! I’m from the FUTURE and I also agree!!!!!

  • Someblackman Onapc Nov 2, 2016 @ 14:41

    Agreed

  • JoeAvrg Jan 5, 2016 @ 22:27

    2016 and I’d still agree to this.

  • snka Aug 28, 2015 @ 23:09

    Thanks Vivek

  • Preben Mar 28, 2015 @ 20:28

    +1 commenting months after is OP.

  • Mijo Jan 14, 2015 @ 10:22

    many many thanx to you……script and command works like charm 🙂

  • christian Nov 20, 2014 @ 15:58

    Awsome thx dude saved my time loads
    x

  • Prashant Nov 10, 2014 @ 10:46

    HI,

    In which folder should this postfix-delete.pl script be kept , as the command results in file not found if kept in root

  • BetaBoy Jul 11, 2014 @ 9:24

    I think he mean the

    } else {
    $queue_id = “”;
    }

    part is added to prevent grepping the dest. server reject message.

  • Mac Oct 15, 2013 @ 22:35

    postsuper -d ALL deferred
    Worked like a champ, I had forgotten the command trying to setup a smart host to relay through and had clogged up the mailq on the server.

    : )

  • Toff Aug 29, 2013 @ 22:41

    mailq |awk ‘{print $1}’ |grep -E “[a-Z 0-9]{13}” |xargs -I {} postsuper -d {}

  • middlewhere Aug 27, 2013 @ 23:40

    saved me hours – thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.