1

I have a cookie

console.log(document.cookie);
Clickme=a-6,a-7,a-8,a-9,a-10,a-17,a-8,

I want to delete this cookie, I have tried following things but they aren't working

document.cookie = "Clickme=; max-age = -1;"
document.cookie =  Clickme+"=; expires=Thu, 01 Jan 1970 00:00:01 GMT;"
document.cookie = "Clickme=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
Learn AspNet
  • 1,192
  • 3
  • 34
  • 74
  • Possible duplicate of [javascript - delete cookie](http://stackoverflow.com/questions/2144386/javascript-delete-cookie) – evolutionxbox Sep 02 '16 at 14:46
  • the second attempt should've worked - if it were correct javascript (so close) `document.cookie = "Clickme=; expires=Thu, 01 Jan 1970 00:00:01 GMT"` - third attempt should've worked too – Jaromanda X Sep 02 '16 at 14:55

3 Answers3

1

I use the following to remove a cookie.

function deleteCookie(name) {
  var domain = location.hostname,
      path = '/'; // root path

  document.cookie = [
    name, '=',
    '; expires=' + new Date(0).toUTCString(),
    '; path=' + path,
    '; domain=' + domain
  ].join('');
}

deleteCookie('Clickme');

Using an array helps me see the parts separately, and using Date(0).toUTCString() guarantees I've got the correct date.

evolutionxbox
  • 3,932
  • 6
  • 34
  • 51
  • This does require that the cookie path was set to the root. In hindsight it might have been better to pass path and domain through as arguments. – evolutionxbox Mar 10 '19 at 10:51
0

Try this. function delete_cookie( name ) { document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;'; }

Hope it works.

Bhushan
  • 46
  • 4
0

See this Delete cookie

You can use this function

function removeItem(sKey, sPath, sDomain) {
    document.cookie = encodeURIComponent(sKey) + 
                  "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + 
                  (sDomain ? "; domain=" + sDomain : "") + 
                  (sPath ? "; path=" + sPath : "");
}

removeItem("cookieName");

In angular you can use $cookies.remove as well, if that helps.

Community
  • 1
  • 1
Muntasir Alam
  • 1,777
  • 2
  • 17
  • 26