24

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?

s.alem
  • 12,579
  • 9
  • 44
  • 72

2 Answers2

33

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);
});
s.alem
  • 12,579
  • 9
  • 44
  • 72
  • 3
    $cookieStore is deprecated use $cookie instead – Prasanth Bendra Mar 25 '15 at 06:51
  • When i am calling above function it getting some other cookies like google and _ga ... What to do to avoid this.. – Sarbanjeet Dec 02 '15 at 14:13
  • The above method removes every cookie which set by your domain. Filtering those cookies is out of this question's scope. But you can simply make a list and check if the cookie key is in that list... – s.alem Dec 02 '15 at 14:52
0

In case you are on this page and you working on an old project that uses angular 1.3.x or less, you can simply use this

$cookies.cookieKey = undefined;
delete $cookies['cookieKey'];
Segun Kess
  • 243
  • 4
  • 7