Removing Large Amounts of Spam from Exchange Server Queue Database

A customer running Exchange 2010 experienced a large number of spam emails in the submission queue (over 80,000).


All the Spam Emails started with "ID#ALERT#", as a result we ran the following command to clean up any emails with "ID#ALERT#" in the subject.

Get-Message -ResultSize unlimited | Where-Object {$_.Subject -match "ID#ALERT#"} | Remove-Message


Due to the significant amount of spam, the command failed to remove the messages and just hung for hours.  However we could remove messages individually.

We could however list the emails with the following command:

Get-Message -ResultSize unlimited | Where-Object {$_.Subject -match "ID#ALERT#"}

This gave us an idea to write a foreach command to remove each email individually from a CSV file.  First we needed a detailed list with the identity of each spam email so we ran the following command:

Get-Message -ResultSize unlimited | Where-Object {$_.Subject -match "ID#ALERT#"} | select Identity > C:\output.txt

We then formatted the CSV file to ensure we specified a name for the Identity column.  As you see we added "Identity" to the top of the file.


We then ran the Remove-Message command multiple times for each Identity in the CSV file using the following command:

Import-Csv "C:\output.txt" | ForEach-Object {Remove-Message -Identity $_.Identity -confirm:$false}

This ran the Remove-Message command over 80,000 times for each message in the queue and was able to clean it up successfully.
Previous
Next Post »