VBScript: Delete ALL E-mails from the Exchange 2003 Queue
Recently my Exchange server got pounded by spammers that were attacking my NDR (non delivery report) capabilities. Turning off NDRs helped 75% and I explored Exchange quite a bit along the way to figure out that last 25%.
It seems that I had 180 emails stuck in my queue. I was looking at my logs and ethereal and it seemed that my mail server would go to several other servers, say "Hello" and then "Goodbye". No message in the middle. It didn't even ask if a recipient was valid. Twenty-four hours later, my queue was still churning at 180 emails constant and Exchange was still being silly.
I took a closer look at the queue. Why was it 180 constant? Then I noticed the date they started their retry -- December 5th 2005. That was the day that installed Exchange (and presumably SP1) on my new server. I restored some PSTs and that's about it. Those emails had been stuck in the queue for well over 2 months! Why didn't they expire? I have no idea.
So I began to try to empty my queue. It was such a manual process. Click on the envelope, click "Find Messages", Delete with No NDR, confirm Yes. I'd have to do this 180 times? No way. So I wrote a script to do it for me: (NOTE! This script empties your ENTIRE queue! There are very few circumstances that you will need to use this script.)
Edit: As noted in the comments below, please close Exchange System Manager before running this script.
' Author: Chrissy LeMaire ' Copyright 2003 NetNerds Consulting Group ' Script is provided AS IS with no warranties or guarantees and assumes no liabilities. ' Website: https://www.netnerds.net ' Description: This scripts empties out the entire Exchange queue. USE WITH CAUTION.
Set objWMIExch = GetObject("winmgmts://./root/MicrosoftExchangeV2") Set objLinksList = objWMIExch.ExecQuery ("Select * from Exchange_SMTPLink") For each objLinkInst in objLinksList
strSQL = "Select * from Exchange_SMTPQueue where " strSQL = strSQL & "LinkID='" & objLinkInst.LinkID strSQL = strSQL & "' and LinkName='" & objLinkInst.LinkName strSQL = strSQL & "' and ProtocolName='" & objLinkInst.ProtocolName strSQL = strSQL & "' and VirtualMachine='" & objLinkInst.VirtualMachine strSQL = strSQL & "' and VirtualServerName='" & objLinkInst.VirtualServerName & "'"
Set objQueuesList = objWMIExch.ExecQuery (strsql) For each objQueueInst in objQueuesList i = i +1 If i > 7 And InStr(objQueueInst.QueueName,".") > 0 Then 'make sure its not the built in stuff
strSQL = "Select * from Exchange_QueuedSMTPMessage where " '<-- This class requires that you pass ALL the variables below in the where clause strSQL = strSQL & "LinkID='" & objLinkInst.LinkID strSQL = strSQL & "' and LinkName='" & objLinkInst.LinkName strSQL = strSQL & "' and ProtocolName='" & objLinkInst.ProtocolName strSQL = strSQL & "' and QueueID='" & objQueueInst.QueueID strSQL = strSQL & "' and QueueName='" & objQueueInst.QueueName strSQL = strSQL & "' and VirtualMachine='" & objLinkInst.VirtualMachine strSQL = strSQL & "' and VirtualServerName='" & objLinkInst.VirtualServerName & "'"
Set objQueuesList1 = objWMIExch.ExecQuery (strsql) For each objQueueInst1 in objQueuesList1 If i > 7 And InStr(objQueueInst1.QueueName,".") > 0 Then objQueueInst1.DeleteNoNDR End If Next End If Next Next
MsgBox i This script emptied my queue in about 25 seconds and Exchange is no longer going out and saying Hello & Goodbye. Traffic is back to normal. I'm assuming that was an Exchange bug.