Click here to Skip to main content
13,149,756 members (82,050 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 22 Oct 2004

Clean out the Cookies folder

, 18 Nov 2004
Rate this:
Please Sign up or sign in to vote.
A command-line utility for cleaning out the Cookies folder.


I was browsing through some folders the other day when I stumbled into the Cookies folder. I was shocked, to say the least, to see several thousand files in there, some of which were nearly three years old. Thinking that something must be wrong with IE's cookie-related settings, I checked IE's properties and everything looked as it should. I clicked on the Delete Cookies button and was presented with a "Delete all cookies in the Temporary Internet Files folder?" message. I responded in the affirmative and then went to check the Cookies folder. It was untouched! All other temporary files had been deleted, however. At this point, I was not sure if the Cookies folder was a special case, or if I was just missing something obvious (it happens). I tried this same thing on a few other machines and got mixed results. On some of them, the Cookies folder was emptied. On others, the folder remained untouched.

Back when I was doing Web development, I seem to remember an Expires attribute of the Cookies collection that was used to set the date on which the cookie would expire. I left that company before we ever got around to verifying whether cookies did all they needed to or not. Maybe, I'm incorrect in thinking that they would automatically get cleaned up when the date indicated by the Expires attribute had passed. Instead, maybe it is simply an indicator to the Web page that the cookie is too old to be used and that another should be created. I welcome any input on this mystery.

That said, I thought I would put together an article on what it would take to automatically clean out the Cookies folder of any old/unused files. The following sections are a brief discussion of that endeavor. Feel free to change the code to fit your needs (e.g., more options, more flexibility, more error checking).

Please note that I did not create this article/utility with the intent of replacing something that IE does internally. It was merely an attempt at addressing what appears to be an inconsistency in how IE manages its Cookies folder. If IE cleans up your Cookies folder with no troubles, this article is not intended for you. If, however, you find that IE does not clean up your Cookies folder, you might just find this article helpful. Enjoy!

Locating the Cookies folder

So, where is the Cookie jar folder anyhow? The easiest way to find out is with the SHGetFolderPath() function.

// call GetBuffer() once instead of potentially calling it twice below
CString strCookiesPath;
LPTSTR lpszPath = strCookiesPath.GetBuffer(MAX_PATH);

// where is the 'jar' of cookies?
                                  NULL, SHGFP_TYPE_CURRENT, lpszPath);
if (FAILED(hResult))
    hResult = SHGetFolderPath(NULL, CSIDL_COOKIES, 
                              NULL, SHGFP_TYPE_DEFAULT, lpszPath);


At this point, strCookiesPath should contain something like "C:\Documents and Settings\<user>\Cookies". To begin searching this folder, all we need to do is tack on a backslash and a pair of wildcard characters, and we're off! Prior to MFC, I would have employed code like the following to do this:

char szPath[256] = "C:\Documents and Settings\<user>\Cookies";
if (szPath[strlen(szPath)] != '\\')
    strcat(szPath, "\\");

This worked as intended, but when I started using MFC back in 1993, I switched to CString objects instead. This resulted in:

strCookiesPath = "C:\Documents and Settings\<user>\Cookies";
if (strCookiesPath.Right(1) != "\\")
    strCookiesPath += "\\";

This was a little bit cleaner but then the problem of localization came up. Hard-coding the backslash character could invite trouble. It has to do with the Right() method. In DBCS languages (e.g., Japanese), this is a common bug because if the last character in the string is double-byte, and the trail byte is 0x5C (the value of backslash), the code will mistakenly think a backslash is already there (thanks Michael Dunn). To address that, I used PathAddBackslash() like:

// add a backslash if necessary

Process the Cookies folder

Our string now contains a trailing backslash. Now we are ready to search (or process) the Cookies folder. To do this, I simply used a CFileFind object. Since the whole premise of this article was to find cookies that had not been accessed in a certain number of days, we simply need to compare the cookie's last-accessed date with some other user-configured date (i.e., cutoff date). If the cookie is old enough, it is added to a list of cookies to be removed, or displayed, later. I am using Michael Dunn's CShellFileOp class for this. This is a nice wrapper around the SHFileOperation() function.

CShellFileOp shfo;
CTime        dateFile,
CFileFind    fileFind;

BOOL bFound = fileFind.FindFile(strCookiesPath + _T("*.*"));
while (TRUE == bFound)
    bFound = fileFind.FindNextFile();

    if (! fileFind.IsDirectory())
        // if the file was last written to before the cutoff date,
        // add it to the list of files to be removed
        if (CTime(dateFile.GetTime()) < dateCutoff)

We now have a list of cookies that can either be deleted, or output to the screen (maybe you want to see the cookies that would be deleted). To show the cookies, just iterate through the list. CShellFileOp provides a method, GetSourceFileList(), to access this list.

const CStringList   &listSourceFiles = shfo.GetSourceFileList();
POSITION            pos;
CString             strFilename;

pos = listSourceFiles.GetHeadPosition();
while (NULL != pos)
    strFilename = listSourceFiles.GetNext(pos);
    cout << (LPCTSTR) strFilename << endl;

If however, we actually wanted to delete the cookies, we would just use the CShellFileOp::Go() method for this. For more on how to use this class, including how to move cookies to the Recycle Bin instead of deleting them, consult Michael's page.

Calculating the cutoff date

The cutoff date can be calculated by simply constructing a CTimeSpan object and subtracting it from the current date. For example, if you wanted to delete cookies that had not been accessed within the last 15 days, you would specify a cutoff date of:

dateCutoff = CTime::GetCurrentTime() - CTimeSpan(15, 0, 0, 0);

Now, if the cookie's last-accessed date is older than the cutoff date, it will get deleted.


Using the utility, aptly named cookiemonster, in its current state, five command-line switches are supported. The handling of these is done via Paul DiLascia's CCommandLineInfoEx class. Here is a synopsis of each:

/days ##Number of days since cookie was last accessed.
/listShow cookies that would be deleted. Cookies are not affected if this is used.
/verboseShow details of what the program is doing.
/recycleMove cookies to the Recycle Bin. Ignored if /list is used.
/?Show the help screen.

Issuing cookiemonster /days 2 /verbose /recycle at a command prompt, which could just as easily be a scheduled task, yielded the following result on my machine:

Deleting cookies last accessed before 10/19/04.
Cookies will be moved to the Recycle Bin.
Deleting cookies from C:\Documents and Settings\davidc\Cookies.
File: davidc@arstechnica[2].txt.
...Date: 10/21/04
File: davidc@arvest[2].txt
...Date: 10/19/04
File: davidc@ask[2].txt
...Date: 10/20/04
...Date: 10/19/04
File: davidc@cardeals[2].txt
...Date: 10/20/04
File: davidc@celebritytrivia[1].txt
...Date: 10/20/04
File: davidc@channelintelligence[2].txt
...Date: 10/21/04
<balance of output deleted for brevity>

Note that two files were added to the list, while five were not because their last-accessed date was newer than the cutoff date.



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

David Crow
Software Developer (Senior) Pinnacle Business Systems
United States United States

The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.

HTTP 404 - File not found
Internet Information Services

You may also be interested in...

Comments and Discussions

GeneralVery practical article! Btw, I have done this in VB :-) Pin
DrABELL16-Sep-09 13:06
memberDrABELL16-Sep-09 13:06 
GeneralMinor mistake Pin
Chris Hills26-Aug-05 9:53
memberChris Hills26-Aug-05 9:53 
GeneralRe: Minor mistake Pin
DavidCrow26-Aug-05 9:57
memberDavidCrow26-Aug-05 9:57 
Generalummm.... Pin
tom_dx19-Nov-04 5:00
membertom_dx19-Nov-04 5:00 
GeneralRe: ummm.... Pin
Mr.Prakash19-Nov-04 6:00
memberMr.Prakash19-Nov-04 6:00 
GeneralRe: ummm.... Pin
tom_dx19-Nov-04 6:03
membertom_dx19-Nov-04 6:03 
GeneralRe: ummm.... Pin
Mr.Prakash19-Nov-04 6:05
memberMr.Prakash19-Nov-04 6:05 
GeneralRe: ummm.... Pin
tom_dx19-Nov-04 6:07
membertom_dx19-Nov-04 6:07 
QuestionBrowser or the Website? Pin
Mr.Prakash19-Nov-04 4:45
memberMr.Prakash19-Nov-04 4:45 
AnswerRe: Browser or the Website? Pin
DavidCrow19-Nov-04 5:55
memberDavidCrow19-Nov-04 5:55 
GeneralNote about the backslash Pin
Michael Dunn16-Nov-04 13:01
sitebuilderMichael Dunn16-Nov-04 13:01 
GeneralRe: Note about the backslash Pin
Anthony_Yio28-Nov-04 19:34
memberAnthony_Yio28-Nov-04 19:34 
GeneralRe: Note about the backslash Pin
Michael Dunn28-Nov-04 20:35
sitebuilderMichael Dunn28-Nov-04 20:35 
GeneralRe: Note about the backslash Pin
Anthony_Yio28-Nov-04 21:10
memberAnthony_Yio28-Nov-04 21:10 
QuestionCannot compile? Pin
Michael Sharkansky31-Oct-04 1:53
sussMichael Sharkansky31-Oct-04 1:53 
AnswerRe: Cannot compile? Pin
DavidCrow1-Nov-04 2:16
memberDavidCrow1-Nov-04 2:16 
GeneralRe: Cannot compile? Pin
Michae Sharkansky1-Nov-04 2:22
sussMichae Sharkansky1-Nov-04 2:22 
GeneralRe: Cannot compile? Pin
DavidCrow1-Nov-04 2:27
memberDavidCrow1-Nov-04 2:27 
GeneralRe: Cannot compile? Pin
Michael Sharkansky1-Nov-04 2:45
sussMichael Sharkansky1-Nov-04 2:45 
GeneralRe: Cannot compile? Pin
DavidCrow1-Nov-04 3:09
memberDavidCrow1-Nov-04 3:09 
GeneralI know what you're saying, ... Pin
WREY22-Oct-04 7:07
memberWREY22-Oct-04 7:07 
GeneralRe: I know what you're saying, ... Pin
DavidCrow22-Oct-04 8:22
memberDavidCrow22-Oct-04 8:22 
GeneralRe: I know what you're saying, ... Pin
WREY22-Oct-04 9:24
memberWREY22-Oct-04 9:24 
GeneralRe: I know what you're saying, ... Pin
DavidCrow22-Oct-04 9:31
memberDavidCrow22-Oct-04 9:31 
GeneralRe: I know what you're saying, ... Pin
WREY22-Oct-04 11:16
memberWREY22-Oct-04 11:16 
GeneralMystery Cookies Pin
Arch4ngel22-Oct-04 4:14
memberArch4ngel22-Oct-04 4:14 
GeneralRe: Mystery Cookies Pin
dorutzu22-Jan-05 6:45
memberdorutzu22-Jan-05 6:45 
GeneralIE Cookies and Performance Pin
Blake Miller22-Oct-04 4:08
memberBlake Miller22-Oct-04 4:08 
GeneralRe: IE Cookies and Performance Pin
Alexander M.22-Oct-04 11:30
memberAlexander M.22-Oct-04 11:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170924.2 | Last Updated 19 Nov 2004
Article Copyright 2004 by David Crow
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid