6

I have a server with multiple domains. How can I clear all Postfix queue messages for a specific domain?

13

This command deletes all mails sent to a recipient address that ends with @example.com

sudo mailq | tail -n +2 | awk 'BEGIN { RS = "" } /@example\.com$/ { print $1 }' | tr -d '*!' | sudo postsuper -d - 
| improve this answer | |
  • 2
    On Linux, use tail -n +2 instead of tail +2. – Brian Minton Jun 12 '17 at 15:28
  • The tr -d can be refactored into the Awk script, too. Replace { print $1 } with { r = $1; gsub(/[!*]/, "", r); print r } – tripleee Oct 26 '18 at 10:07
7

I have tried this solution in ubuntu 12.04, and it doesn't work this way:

sudo mailq | tail +2 | awk 'BEGIN { RS = "" } / @example\.com$/ { print $1 }' | tr -d '*!' | sudo postsuper -d -

I need to change to this way:

postqueue -p | tail -n +2 | awk 'BEGIN { RS = "" } /@example\.com/ { print $1 }' | tr -d '*!' | postsuper -d -
| improve this answer | |
  • yes, you have to eliminate the space before the "@". – Alex Nov 23 '17 at 12:52
6

Grep solution

mailq | grep example.com -B1 | grep -oE "^[A-Z0-9]{10,11}" | sudo postsuper -d -

assumes ID is between 10 and 11 digits, (based on inodes)

| improve this answer | |
1

Look at pfdel.pl, a mandatory tool to manage the queue. It takes a regexp and remove the mails waiting in queue corresponding to your domain.

| improve this answer | |
0

When you want to delete messages from or to e-mail addresses at a specific domain, this command works for me:

mailq | \
  tail -n +2 | \
  awk 'BEGIN { RS = "" } / @example\.com$/ { print $1 }' | \
  tr -d '*!' | \
  postsuper -d -

Also works for deleting e-mails from or to specific e-mail addresses by supplying for example mail@example\.com$/ instead of @example\.com$/.

Taken from a comment on howtoforge.com. See there for related solutions and the command in one line. (I used bash line continuation for readability).

A very similar command that allows to make deletion dependent on whether the address appears as sender, recipient etc. is found in man postsuper, where it says about -d:

For example, to delete all mail with exactly one recipient user@example.com:

mailq | \
  tail -n +2 | \
  grep -v '^ *(' | \
  awk 'BEGIN { RS = "" } { if ($8 == "user@example.com" && $9 == "") print $1 }' | \
  tr -d '*!' | \
  postsuper -d -

(The variables mean: $7=sender, $8=recipient1, $9=recipient2. I changed the quote to use tail -n +2, since their tail +2 does not work any more, at least on some modern systems.)

| improve this answer | |
  • The space before @example\.com seems wrong. – tripleee Oct 26 '18 at 10:04
  • 1
    The backslashes at the end of line are not actually necessary; the shell understands that the command continues on the next line if the last token is | (or || or && or a bunch of others). – tripleee Oct 26 '18 at 10:10
-2

I've modified it little bit:

mailq | grep -B1 | grep -oE "^[A-Z0-9]{12}" | xargs -I% postsuper -d %

| improve this answer | |
  • 2
    Then please explain why and what this is supposed to do. – Sven Apr 12 '18 at 15:40
  • Running hundreds of instances of postsuper is not an improvement at all. xargs is nice when you use it properly, but this is not one of those situations. – tripleee Oct 26 '18 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.