How to remove all cookies in Angularjs?

s.alem picture s.alem · Oct 28, 2014 · Viewed 26.2k times · Source

I can set a cookie like this:

$cookieStore.put('myCookie','I am a cookie');

And I can remove it with

$cookieStore.remove('myCookie');

But how can I remove all cookies?

Answer

s.alem picture s.alem · Oct 28, 2014

Ok, obviously this may not be the best solution, but I've find a workaround:

angular.forEach($cookies, function (v, k) {
    $cookieStore.remove(k);
});

But I'ld still appreciate if there's a better solution. I'm really curious about why there isn't a built-in $cookieStore.removeAll() method...

Requires the ngCookies module to be installed.

Edit

With the 1.4 version, $cookieStore is deprecated. Instead you can use $cookies service. Get all cookies with $cookies.getAll() and remove each with $cookies.remove('key').

var cookies = $cookies.getAll();
angular.forEach(cookies, function (v, k) {
    $cookies.remove(k);
});