• Category Archives Postfix
  • Linux » Postfix
  • How to Purge the postfix Mail Queue

    To clear the mail queue in Postfix, we will use the command called postsuper . “postsuper” is short for Postfix superintendent. The postsuper command is used for postfix mail queue related maintenance work and can only be run as a superuser (root).
    To check the mail queue in system, we use mailq command . Using the mailq command will show a list of e-mail messages that have not been delivered. Most of the messages will have a status of “deferred” and sit in the deferred queue .

    First run the command mailq command to check how many messages are in queue overall.

    There are three likely scenarios that you will need to blow away the queue:

    Scenario 1 : Remove particular mail queue id (on running mailq command,you will get mail queue id)

    postsuper -d mail_queue_id
    

    Scenario 2: Remove ALL messages from all queue

    postsuper -d ALL
    

    Scenario 3: Remove only ALL deferred messages which are in the deferredqueue

    postsuper -d ALL deferred
    

  • Flush Postfix 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

    #!/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