Script to delete specific IE cookie

Posted on Updated on

We had a recent need to delete IE cookies from a specific website throughout the organization.

' NAME: DeleteIeCookies
' AUTHOR: Nick Moseley ,
' 1.0 (08/11/2011) - Initial script
' 2.0 (11/10/2011) - Updated to support Win7 vs. WinXP
' COMMENT: Deletes the cookies specified in variable CookieName.  To use, 
' set variable "CookieName" to what you need to delete.
Option Explicit

Const CookieName = "yahoo"
Const ForAppending = 8
Const HKLM = &H80000002
Const EnableLogging = True
Const LogFile = "C:\DeleteIeCookies.log"

' Basic objects
Dim oReg, oShell, oFSO, iCountFiles
Set oReg = GetObject("winmgmts:\\.\root\default:StdRegProv")
Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject ("Scripting.FileSystemObject")
iCountFiles = 0

' Get user folder location for Vista/7 vs. XP
Dim sOS
If oFSO.FolderExists ("C:\Users") Then
 sOS = "win7"
 sOS = "xp"
End If

' Begin log file etnries
If EnableLogging = True Then 
 Dim oVersionLog
 If Not oFSO.FileExists(LogFile) Then
  oFSO.CreateTextFile LogFile
 End If
 Set oVersionLog = oFSO.OpenTextFile (LogFile, ForAppending, True)
 oVersionLog.WriteLine "Delete Craigslist cookies"
 oVersionLog.WriteLine "  => Began at " & Date & " " & Time
End If

' Get list of folders in C:\Users
Dim oUserFolders, oFolder, colFiles, oUserCookieFolder, oFile
If sOS = "win7" Then
 Set oUserFolders = oFSO.GetFolder("C:\Users") 
 Set oUserFolders = oFSO.GetFolder("C:\Documents and Settings") 
End If

For Each oFolder In oUserFolders.subfolders 
 If sOS = "win7" Then
  DeleteFile ("C:\Users\" & & "\AppData\Roaming\Microsoft\Windows\Cookies") 
  DeleteFile ("C:\Users\" & & "\AppData\Roaming\Microsoft\Windows\Cookies\Low") 
  DeleteFile ("C:\Documents and Settings\" & & "\Cookies") 
  DeleteFile ("C:\Documents and Settings\" & & "\Local Settings\Temporary Internet Files") 
 End If

' End logging
If EnableLogging = True Then 
 oVersionLog.WriteLine "  => Total number of deleted cookies: " & iCountFiles
 oVersionLog.WriteLine "  => Completed at " & Date & " " & Time
End If


Sub DeleteFile (sFolderPath)
 If oFSO.FolderExists (sFolderPath) Then
  Set oUserCookieFolder = oFSO.GetFolder(sFolderPath)
  Set colFiles = oUserCookieFolder.Files
  For Each oFile In colFiles
   If InStr (oFile.Name, CookieName) Then 
    'WScript.Echo "file: " & oFile.Name
    If EnableLogging = True Then
     oVersionLog.WriteLine "  => Deleted " & sFolderPath & "\" & oFile.Name
     'WScript.Echo sFolderPath & "\" & oFile.Name
    End If
    oFSO.DeleteFile sFolderPath & "\" & oFile.Name
    iCountFiles = iCountFiles + 1
   End If
 End If 
End Sub

8 thoughts on “Script to delete specific IE cookie

    Bill said:
    November 10, 2011 at 6:31 am

    Hi there,

    I am a newbie at Scripting, so please bear with me. I need to delete a specific cookie, anything with “”, e.g. (IE7) wherever it may reside on every XP machine. Will this script accomplish this? If not, as written, what would I need to modify to make it do this?

    I would appreciate your earliest convenient reply!

    Thanks, and enjoy the day!


    Bill said:
    November 10, 2011 at 8:41 am

    This also must inlcude any user of the machine.



      Nicolas Moseley responded:
      November 10, 2011 at 9:00 am

      Bill, I updated my code for WinXP to test it, but it’s not locating the cookies properly. Unfortunately I cannot put much more time into it this week, so if you need it right away, I suggest engaging someone within your organization who knows VBScript. Otherwise I can troubleshoot this over the weekend if time permits.

        Bill said:
        November 10, 2011 at 12:51 pm

        Hi there,

        Yeah, that’s what I thought it was doing, as well. I tried replacing the path as it was here with the actual path in Win XP (“C:\Documents and Settings\”user”\Local Settings\Temporary Internet Files”), but couldn’t get it to work.

        Did you put that updated code anywhere, or just test it locally?

        I have been checking with colleagues, however, unfortunately, we are a small group and we’ve all be loaded down recently with urgent gigs, this, apparently being one of mine ;). Any assistance you can provide would be greatly appreciated. I’ll check back later and tomorrow in case a little light goes off in yer head before tomorrow.

        Thanks, bud, I appreciate it and enjoy the evening!


        Nicolas Moseley responded:
        November 11, 2011 at 11:01 am

        Bill, I have updated and uploaded version 2 of my script to support WinXP as well. Not sure why, but the script works on some cookies but not others. For example, it didn’t work for “yahoo” but it did work for “whoscalling”. So try it again in your environment. Remember too that whatever computer you’re running this on, that the logged in user is a local admin.

    Bill said:
    November 12, 2011 at 9:07 am

    Thanks, bud. just downloaded it, and about to test. Will let you know how it works.


    Bill said:
    November 16, 2011 at 6:36 am

    Hi there,

    Still cannot get it to delete from \Temporary Internet Files\. It delets only from 1st \Cookies Folder. I’ve REM’d the check in the first Cookies folder, to make sure it is only checking the Temp Internet Files, but nio go. It works fine on C:\Documents and Settins\(user)\Cookies, just not the rest.


    James said:
    May 31, 2012 at 6:31 am

    Hi admin,
    I want del cookie on IE, i try this command:
    Run(“RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255”)
    But it only apply when i exit IE and run IE again, please help me to apply immediately on the window IE running.
    Thanks you.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s