We had an issue in a development server which sends thousands of mail messages to the system administrator. Those mails were queued in the out going mail server which affected the normal mail delivery process in the out going mail server.
I used following command to remove unwanted mails from the mailq.
mailq |fgrep apache@testdomain.nic.lk|sed 's/\*.*//'| postsuper -d -
Here apache@testdomain.nic.lk is the mail sending user and I did not use the recipient address to filter the mails as he might delete required mails also.
When I wanted to delete the mails received to a mail user (i.e. rcpt to:someone@yourdomain.lk) I used the following command to delete them:
mailq | awk 'BEGIN { RS = "" } / someone@userdomain\.lk$/ { print $1 }' | postsuper -d -
Clarification
fgrep will display the detailed line of the mails sent by 'apache@testdomain.nic.lk' which are queued in the mailq and using sed command, just the mailIDs can be listed.
awk is also such tool which can be used for text formatting. When it used correctly with relevant switches. it will generate a list which can be used as an input to postsuper process to delete the given mailID.
Normally, postsuper -d <MailID> will delete the mail in the queue with given mail ID. So the above command will list all mailIDs in the mailq send by a particular user and forward to delete it with postsuper user command.
Good To Read:
https://www.howtoforge.com/delete-mails-to-or-from-a-specific-email-address-from-postfix-mail-queue
I used following command to remove unwanted mails from the mailq.
mailq |fgrep apache@testdomain.nic.lk|sed 's/\*.*//'| postsuper -d -
Here apache@testdomain.nic.lk is the mail sending user and I did not use the recipient address to filter the mails as he might delete required mails also.
When I wanted to delete the mails received to a mail user (i.e. rcpt to:someone@yourdomain.lk) I used the following command to delete them:
mailq | awk 'BEGIN { RS = "" } / someone@userdomain\.lk$/ { print $1 }' | postsuper -d -
Clarification
fgrep will display the detailed line of the mails sent by 'apache@testdomain.nic.lk' which are queued in the mailq and using sed command, just the mailIDs can be listed.
awk is also such tool which can be used for text formatting. When it used correctly with relevant switches. it will generate a list which can be used as an input to postsuper process to delete the given mailID.
Normally, postsuper -d <MailID> will delete the mail in the queue with given mail ID. So the above command will list all mailIDs in the mailq send by a particular user and forward to delete it with postsuper user command.
Good To Read:
https://www.howtoforge.com/delete-mails-to-or-from-a-specific-email-address-from-postfix-mail-queue