Cookies are actually files containing data that is used by websites to remember user preferences and other relevant information, such as authentication credentials. In some cases, we may need to delete these cookies in order to do the following:
There are several ways to achieve deletion of cookies, but here we will show the simplest way with the undocumented WebUtil
object and methods.
Let's take an example. From the File menu, navigate to New | Test, use the Ctrl + N shortcut, or use the Web_Functions.vbs
library you created in the previous recipe, to encapsulate the commands in your own custom functions.
To delete all cookies, write the following code:
WebUtil.DeleteCookies()
To delete a specific cookie from a domain, write the following code:
WebUtil.DeleteCookie(Domain, Cookie)
To encapsulate the commands in custom functions, write the following functions in the function library:
Function DeleteCookies() WebUtil.DeleteCookies() End Function Function DeleteCookie(ByVal Domain, ByVal Cookie) WebUtil.DeleteCookie(Domain, Cookie) End Function
The previous code is self-evident. The first statement is equivalent to choosing to delete cookies through the IE Internet Options in the Control Panel window by navigating to Browsing history under the General tab, and then clicking on the Delete… button. This opens a Delete Browsing History dialog. The second statement deletes a specific cookie for a given domain. In that case, the name of the cookie must be accurate.