VBScript: Delete ALL E-mails from the Exchange 2003 Queue

Filed under: Exchange, Quick Code, VBScript — Written by Chrissy on Sunday, April 16th, 2006 @ 5:06 pm

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.)

' Author: Chrissy LeMaire
' Copyright 2003 NetNerds Consulting Group
' Script is provided AS IS with no warranties or guarantees and assumes no liabilities.
' Website: http://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.

44 Comments   -
  • Comment by Moh'd Sudqi | June 3, 2006 @ 1:44 pm

    dear sir,

    please send me, which values in this script should be altered, because I ran the script and get run time error message

    Best Regards
    Moh'd Sudqi

  • Comment by Chrissy | June 3, 2006 @ 3:17 pm

    Moh'd,
    You must run this script on the Exchange machine with administrator privleges. If you wish to run the script from a remote machine, you must still have administrative privleges on the Exchange server but you can change the "." in

    GetObject("winmgmts://./root/MicrosoftExchangeV2")

    to the name of your Exchange server. For example:

    GetObject("winmgmts://myexchangeserver/root/MicrosoftExchangeV2")

  • Comment by Moh'd Sudqi | June 4, 2006 @ 7:36 am

    Dear Sir,

    I have an administrator privleges on my exchange server and my exchange server name is amoon, I put it intstead of “.”, but I got the same result :" run time error , Automation error".

    Thank you for your cooperation.

    Regards
    Moh'd Sudqi

  • Comment by Moh'd Sudqi | June 5, 2006 @ 7:43 am

    Chrissy,

    Still waiting!!!!!!!!!!!!!!!!!!!!

  • Comment by Chrissy | June 5, 2006 @ 5:48 pm

    Err Moh'd. Don't forget that I'm under no obligation to help you. Don't be a dick.
    I've tested the script on my Exchange 2003 machine and it works. You can try deleting all of the files within C:\Program Files\Exchsrvr\Mailroot\VSI 1\Queue. If that doesn't work, I don't know what to tell you.

  • Comment by Moh'd Sudqi | June 6, 2006 @ 6:12 am

    NEVER MIND SIR,
    by the way my Exchange is Exchange 2000, and the script doesn't work as you told

    thanks

  • Comment by Chris | June 12, 2006 @ 2:19 pm

    Hey Chrissy,

    Does this script also clear out the Send Queue (as found in perfmon on the MSEXchange IS Private/Send Queue Size). The problem I am having is the Send Queue shows a constant number of messages in it, but when I look at the queues in Exchange Admin, I do not see any messages stuck anywhere.

    I am using Exchange Enterprise 2003.

  • Comment by Sachin | August 23, 2006 @ 9:34 am

    Hi ,
    I am running this script on Exchange server with administrative privalges. I am getting error as

    WbemobjectEx not found.

    Did you get such error before?
    Please let me know about this.
    Regards,
    Sachin

  • Comment by newengland | September 19, 2006 @ 2:02 am

    Hey Chrissy,

    The script worked for me! thanks,

  • Comment by Andrew | October 2, 2006 @ 2:12 am

    Hi Chrissy!
    This worked for me.
    But i did have to do the following:
    add 'on error resume next' to the beginning
    run the script
    wait a bit
    manually kill the scripting host (wscript.exe)
    restart smtpsvc

    A bit rough, but it succeeded in getting my queue back.

    Thanks for this!

  • Comment by Andrew Badge | November 8, 2006 @ 2:50 pm

    Excellent work Chrissy.
    This saved me alot of time during a period where i didn't have alot ;-)
    Thanks
    Andrew

  • Comment by Phil | November 8, 2006 @ 4:07 pm

    Dear Chrissy:

    I cannot thank you enough for sharing this script. I had a server with thousands of items in the outbound SMTP queue because of a reverse NDR Spam attack. Your script allowed me to quickly delete all the outbound NDR messages and restore server operation. Thanks!!!

    Phil

  • Comment by philip | December 4, 2006 @ 4:56 am

    when using your script i get the following error message irgendwas.vbs(33, 50) (null): 0xC00402BE can you help me?

  • Comment by Chrissy | January 17, 2007 @ 11:08 pm

    Hey all
    Sorry about the suuuuuper late responses. Let's see...

    Chris, I'm not an expert with Exchange but judging from the object naming in the script, I'd imagine that it will clear out mails. I know its six months later but if it worked, please let me know ; )

    Sachin,
    I initially thought that problem would be caused by running the script on a server w/o Exchange but I just ran it on my Exchangeless laptop which runs Windows 2003 and I had no problems. What OS are you running it on?

    newengland,
    Thanks for the note! I'm glad it helped!

    Andrew,
    Glad you got it to work! I'll add that line at the top one day when I'm not so lazy ; )

    Other Andrew & Phil,
    Hell yeah! Glad to hear it :- D

    Phillip,
    I just tried looking up that error and really came up with nothing. Try restarting Exchange and all its subservices? ; )

  • Comment by Sanjeev | January 20, 2007 @ 9:34 pm

    Hi Chris,

    Your script helped me a lot, my server was attacked by spams,
    their where many queues. But with your script i cleared it in 10 Sec.

    Thanks a lot...

  • Comment by Ray | January 26, 2007 @ 10:36 am

    I am getting the error code 0xC1032786

  • Comment by Chrissy | January 28, 2007 @ 7:51 pm

    Glad I could help, Sanjeev!

    Sorry Ray but I have no idea.. I can't even find that error on de 'net.

  • Comment by Ray | January 31, 2007 @ 9:45 am

    Here are more details.

    Line 33
    Chr 50
    code 0xC1032786
    Error c1032786
    Source Null

    Chrissy--does the script work as is?

  • Comment by Chrissy | January 31, 2007 @ 10:01 am

    Hey Ray,
    Yes, it does work as is (as seen in some of the comments.) But I haven't tested it on anything other than Exchange 2003/Win2k3 with Admin Privs. I still can't find any information about your error. What is your setup like? Are you a domain admin? What version is your exchange server? OS?

  • Comment by Ray | January 31, 2007 @ 10:32 am

    Hi Chrissy,

    I am hosting exchange 03 ver 6.5. I am the domain admin and executing the scritp via rdc thus that would require me to modify the
    GetObject("winmgmts://./root/MicrosoftExchangeV2") ..right?

    The only thing I have not done is restarting the iisadmin services.....is this required?

    Thanks.

  • Comment by James | February 13, 2007 @ 2:03 pm

    I have tried the script on my Server but have not gotten to to work yet. I am running Windows 200 Enterprise and Exchange 2003.

    I am the admin and login in as an administrator for exchange.

    I get a Message Box saying 22, which I assume this is the I variable, but I do not see my queue empty.

    Does it matter what state the queues are in?

  • Comment by Dan | February 15, 2007 @ 10:37 pm

    BTW.. if you want this script to not crash.. DNO NOT LEAVE exchange system manager open when u run it

    .. i found this out....

    -D

  • Comment by Joel | March 7, 2007 @ 7:28 pm

    Thanks for the script... worked on a exchange 2003 cluster setup. Great work.

  • Comment by Stuart | April 1, 2007 @ 8:11 pm

    Hi Chrissy,

    Just wanted to say thanks for sharing this script. It ran just fine on my Windows 2003 SBS server and saved me a lot of time.

    Thanks again,
    Stuart

  • Comment by fabbers | April 13, 2007 @ 5:19 am

    Cheers for this, worked a treat.

  • Comment by Droopie | May 25, 2007 @ 1:53 pm

    Thank you for sharing the script...,works for me fine Chrissy

    Thanks

    Peter Jonkers
    Netherlands

  • Comment by Jim Sourbeer | June 6, 2007 @ 6:26 am

    If you are unable to get the script to work, MS has a KB on the procedure for deleting all the messages. You will need to create a temp smtp connector, once all messages get sent to the connector you can then clear them out.

    http://support.microsoft.com/default.aspx?kbid=909005

    --Jim

  • Comment by Jim Sourbeer | June 6, 2007 @ 6:33 am

    Look for step 3 in the kb btw.

    --Jim

  • Comment by David | June 26, 2007 @ 1:49 am

    Thanks Chrissy, that worked a treat! :)

  • Comment by Bob | June 28, 2007 @ 6:39 pm

    HEllllp !

    we are not techie guys, we have had a worm attack on our exchange dserver 2000.

    Ques ant clearing
    we cant send out email
    we can recive
    we now have AVAST and its cleaned it up but we cant get the mail send worked out.

    any help would be apreciated

    Bob

  • Comment by Bryan | June 30, 2007 @ 6:38 am

    Jim Sourbeer, Thanks for the great tip on step3 of kb article 909005. We've had 2712 NDR's stuck in our outgoing queue since our Exchange 2003 crashed a couple of months ago. This cleared it right up.

  • Comment by Scott | July 8, 2007 @ 11:39 pm

    Microsoft has a downloadable commandline utility that is an executable available from their website. It is tricky in syntax but can do everything!

    Search for aquadmcli.exe

    If you run it interactively...

    &gt;setserver frontend
    &gt;delmsg flags=sender,sender=postmaster@domain.com

    I have used this flawless utility for years

    Scott.

  • Comment by Scott | July 8, 2007 @ 11:41 pm

    Sorry that is aqadmcli.exe in my previous post,

    Scott.

  • Comment by JonInSydney | August 5, 2007 @ 8:38 am

    Hi Chrissy,

    Many thanks for this script.

    There was an error message but it still worked for me.

    To be more specific, there was 3 out of 180 queues that weren't cleared, and once i reset iisadmin the retry queues were cleared off.

    Cheers

  • Comment by ebwler | August 13, 2007 @ 8:05 pm

    Hi,

    Sorry, but aqadmcli.exe can't do everything. It doesn't suppport asterisk and you can't search in subject.

    How can I delete specific messages from queue (Exchange 2003) by filtering in subject or sender without the "ultra fast" Microshit Exchange System Manager? Are there any scripts or shell tools to do this?

    I don't want to stop smtp service evry time and create a new queue folder.

    I'm looking for a tool like aqadmcli.exe but with more filter functions.

    Thanks.

    --
    ebwler

  • Comment by Tariq | September 5, 2007 @ 2:17 pm

    Dear All,

    How i run this Script? Please tell me as soon as possible.

  • Comment by Aami | September 25, 2007 @ 2:44 pm

    Can anyone tell me that how to run this script. i am not programer so please tell me how to run this script

  • Comment by Rahim | October 15, 2007 @ 9:09 pm

    Hey....

    So i dont know if i am running this script correctly.
    i saved it in a test file with the etention .vbs , then i ran it with dos mode. i didnt get errors . am i doing this correctly . im new to scripts

  • Comment by Katze | October 22, 2007 @ 6:31 pm

    Thanks! This worked like a charm to wipe our queues. I had a user send out around 5,000 emails containing 4+ meg attachments. This resulted in a HUGE bottleneck in our exchange queue. I wasn't looking forward to clearing that many queues of his idiocy at once. This script worked like a charm (once I closed ESM) and made my life better.

  • Comment by Muneer Baloch | January 23, 2008 @ 12:16 pm

    Thanks a lot Chrissy. Your VBscript saved me lots of time. I had 1500 spam NDR emails on my external email outbound queue & i was actually sitting & deleting the NDR spam email under each domain name.

    God bless you Chrissy . You saved my day.

  • Comment by Fredrik Sätherström | March 7, 2008 @ 4:09 pm

    Hi Chrissy,

    Just wanted to say thanks for sharing this script. It ran just fine on my Windows 2003 SBS server and saved me a lot of time.

    Thanks again,
    Stuart

  • Comment by Ross M | March 9, 2008 @ 3:01 am

    Chrissy,

    Worked a treat for me!!!

    Cheers!!

  • Comment by Alex | April 2, 2008 @ 1:28 am

    Thanks so much!

    Worked 100% first time with no changes to the original script.

    Running 2003 SBS SP2

    You just saved me days of pain...

  • Pingback Clearing Exchange 2003 SMTP Queues « Andys Techie Blog | April 22, 2008 @ 7:26 am

    [...] I found this post by Chrissy LeMaire most helpful. She has a vbscript that you can use to completely wipe out ALL the messages in ALL queues. [...]

Leave your comment