Removing mail with the same subject from the Exchange Queues

Sometimes you need to remove thousands of e-mails in your Hub Transport server queues. We see this every now and then with mail storms from an application that went off the rails or maybe with 2 mailboxes sending each other auto replies causing a mail loop.

There’s a few ways to stop the mail storm before removing the mail items in your Exchange queues but that is not the scope for this article. If you are ever in the situation of thousands of mails queued on all your Hub Transport servers then the below cmdlet should help you to quickly remove those mails from the queue. (Obviously do this with mails that are not classified as normal business or valid mail i.e. auto response loops)

  1. Get-TransportServer HubTransportServerName| Get-Queue | Get-Message -ResultSize unlimited | where {$_.Subject -eq “Paste the subject you need removed here.”} | Remove-Message -WithNDR $False

a better way might be #2 below if you don’t want to confirm deletion and have to run it across all your Hub Transport Servers: (Thanks to Clinton Hardenberg for the updated cmdlet)

2. Get-TransportServer HTServer0* | Get-Queue |where {$_.MessageCount -gt 25} | Get-Message -ResultSize unlimited | Where {$_.Subject -eq “Paste the subject you need removed here.”} |            Remove-Message -WithNDR $False -Confirm:$false

The 2nd cmdlet will use the 1st part of your server names i.e. if you have HT01, HT02 and HT03 then use HT0* to remove the mail across all your HT servers. The -WithNDR $False will delete the mail and not send an NDR which is important in this case as you will just increase the number of mails when it’s set to $True. -Confirm $False negates the need to select “A” and confirm that you want to remove the mail items from the queues.

There are other parameters you can use to define which items you want to remove but in the above examples we use the Subject as we deal mostly with mail storms by Subject as they are usually

 


Leave a Reply