Clear all cookies

459 views
Skip to first unread message

werehamster

unread,
Aug 4, 2011, 3:59:01 PM8/4/11
to Chromium-extensions
The cookies API allows you to remove individual cookies (http://
code.google.com/chrome/extensions/cookies.html#method-remove), and it
also allows you to list all cookies (using getAll).

I want to remove all cookies in one go, I suspect I can just run
through them one at a time, but this seems very inefficient. Does
anyone know of a more appropriate way of clearing all your cookies?

Frédérik Labbé

unread,
Aug 4, 2011, 4:51:33 PM8/4/11
to Chromium-extensions
(http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/
extensions/docs/examples/api/cookies/manager.html?content-type=text/
plain)

I think that this method is the fastest one to clear all your
cookies :

function removeAll() {
var all_cookies = [];
cache.getDomains().forEach(function(domain) {
cache.getCookies(domain).forEach(function(cookie) {
all_cookies.push(cookie);
});
});
cache.reset();
var count = all_cookies.length;
var timer = new Timer();
for (var i = 0; i < count; i++) {
removeCookie(all_cookies[i]);
}
timer.reset();
chrome.cookies.getAll({}, function(cookies) {
for (var i in cookies) {
cache.add(cookies[i]);
removeCookie(cookies[i]);
}
});
}

function removeCookie(cookie) {
var url = "http" + (cookie.secure ? "s" : "") + "://" +
cookie.domain +
cookie.path;
chrome.cookies.remove({"url": url, "name": cookie.name});
}

function removeCookiesForDomain(domain) {
var timer = new Timer();
cache.getCookies(domain).forEach(function(cookie) {
removeCookie(cookie);
});
}

Hope it helps

werehamster

unread,
Aug 6, 2011, 4:51:32 PM8/6/11
to Chromium-extensions
Thanks for the code example Frédérik, and indeed that is similar to
how I was planning on achieving it if I had to go down that route, but
as I said it looks _very_ inefficient.
What I'm asking is "Is there a more efficient was of clearing all the
cookies in one go?", or even clearing all the cookies in a given store
in one go.

Karl
Reply all
Reply to author
Forward
0 new messages