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

Chrissy is a PowerShell MVP who has worked in IT for nearly 20 years, and currently serves as a Sr. Database Engineer in Belgium. Always an avid scripter, she attended the Monad session at Microsoft’s Professional Developers Conference in Los Angeles back in 2005 and has worked and played with PowerShell ever since. Chrissy is currently pursuing an MS in Systems Engineering at Regis University and helps maintain RealCajunRecipes.com in her spare time. She holds a number of certifications, including those relating to SQL Server, SuSE Linux, SharePoint and network security. She recently became co-lead of the SQL PASS PowerShell Virtual Chapter. You can follow her on Twitter at @cl.

Posted in Exchange, VBScript
49 comments on “VBScript: Delete ALL E-mails from the Exchange 2003 Queue
  1. Moh'd Sudqi says:

    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

  2. Chrissy says:

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

  3. Moh'd Sudqi says:

    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

  4. Moh'd Sudqi says:

    Chrissy,

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

  5. Chrissy says:

    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.

  6. Moh'd Sudqi says:

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

    thanks

  7. Chris says:

    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.

  8. Sachin says:

    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

  9. newengland says:

    Hey Chrissy,

    The script worked for me! thanks,

  10. Andrew says:

    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!

  11. Andrew Badge says:

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

    Thanks
    Andrew

  12. Phil says:

    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

  13. philip says:

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

  14. Chrissy says:

    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? ; )

  15. Sanjeev says:

    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…

  16. Ray says:

    I am getting the error code 0xC1032786

  17. Chrissy says:

    Glad I could help, Sanjeev!

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

  18. Ray says:

    Here are more details.

    Line 33
    Chr 50
    code 0xC1032786
    Error c1032786
    Source Null

    Chrissy–does the script work as is?

  19. Chrissy says:

    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?

  20. Ray says:

    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.

  21. James says:

    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?

  22. Dan says:

    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

  23. Joel says:

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

  24. Stuart says:

    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

  25. fabbers says:

    Cheers for this, worked a treat.

  26. Droopie says:

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

    Thanks

    Peter Jonkers
    Netherlands

  27. Jim Sourbeer says:

    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

  28. Jim Sourbeer says:

    Look for step 3 in the kb btw.

    –Jim

  29. David says:

    Thanks Chrissy, that worked a treat! :)

  30. Bob says:

    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

  31. Bryan says:

    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.

  32. Scott says:

    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,[email protected]

    I have used this flawless utility for years

    Scott.

  33. Scott says:

    Sorry that is aqadmcli.exe in my previous post,

    Scott.

  34. JonInSydney says:

    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

  35. ebwler says:

    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

  36. Tariq says:

    Dear All,

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

  37. Aami says:

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

  38. Rahim says:

    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

  39. Katze says:

    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.

  40. Muneer Baloch says:

    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.

  41. Fredrik Sätherström says:

    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

  42. Ross M says:

    Chrissy,

    Worked a treat for me!!!

    Cheers!!

  43. Alex says:

    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…

  44. Rick H says:

    This thing worked like a champ.

    Thanks

  45. Luis Guzman says:

    I need this script which will delete all my queues in my windows exchange 2003 please send.

  46. Danny says:

    Dear Chrissy,

    When i run it shown an error at line (49,9) “Unterminated entity reference matching “;” not found”. Could you please explain this for me? I run it at command prompt with “C:\Windows\System32\cscript.exe C:\delqueue.wsf”
    Your replay is highly appreciated.

    regards,’
    Danny

  47. Zac says:

    You are amazing.

    Thanks

  48. Cornelis says:

    In other words: THANK YOU VERY MUCH :)

  49. Cornelis says:

    Hi there,

    I had exactly the same problem except there were 1625 queues “stuck” after a user managed to send a malformed e-mail somehow. All the other valid e-mails were getting queued and delayed behind these and the darn thing wasn’t expiring, instead it kept sending NDRs back to the user yet retry sending again and NDR again, retry again … (see where I’m going?) After about 10,000 NDRs (they tried sending to 3000+ recipients), they finally decided to call us and we noticed the backlog.

    I thought I had to clear them manually cos of being too afraid to play with VBscript in a live environment when I’m unexperienced with using the Exchange WMI controls via scripts. After about an hour though I thought I’d rather try and die than click my mouse 24,375 times! (I calculated it…)

    Your script served as a great example though and after small modifications for seeking out that specific Message-ID, the script managed to selectively delete the problematic e-mails from all the queues in less than 10 minutes!

    Awesome. You saved me serious RSI from clicking the mouse too often. Also about 10 hours overtime!

3 Pings/Trackbacks for "VBScript: Delete ALL E-mails from the Exchange 2003 Queue"
  1. […] 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. […]

  2. […] is a script here that will clear the entire queue for you, using vbscript. Are you looking to do that, or to remove […]

  3. […] is a script here that will clear the entire queue for you, using vbscript. Are you looking to do that, or to remove […]

Leave a Reply

Your email address will not be published. Required fields are marked *

*