How to clear cookies in angular.js

69.8k Views Asked by At

Currently am using angulajs app.

I want to store some values in cookie. So i used angular-cookies-min script for add some values to cookies

I have used this below code for save values to cookie.

 $cookieStore.put("userInfo", userInfo);//userInfo is a array with some values. 

Now I want to clear the cookie? How can i do it?

3

There are 3 best solutions below

5
On BEST ANSWER

Try this code for delete cookie

$cookieStore.remove("userInfo");

EDIT: Since v1.4 $cookieStore has been deprecated (see docs), so from that version on you should use:

$cookies.remove("userInfo");
0
On

I have found some answer

Option 1

delete $cookies["userInfo"]

option 2

 .controller('LogoutCtrl', ['$scope',  '$cookies', function ($scope,$cookies) {

    $cookies.userInfo = undefined;

}]);
0
On
angular.forEach($cookies, function (cookie, key) {
    if (key.indexOf('NAV-') > -1) {
         $window.sessionStorage.setItem(key, cookie);
         delete $cookies[key];
    }
 });

The above code works for me however 'Resources' tab of Chrome inspection utility continues to show that the cookies are not deleted. I verified that the cookies are indeed deleted by printing them out:

console.log('START printing cookies AFTER deleting them');
 angular.forEach($cookies, function (cookie, key) {
    console.log(key + ': ' + $cookies[key] + '\n');
 });
 console.log('DONE printing cookies AFTER deleting them');

Hope this helps