202

How can I delete a specific cookie with the name roundcube_sessauth?

Shouldn't the following:

function del_cookie(name) {
    document.cookie = 'roundcube_sessauth' + 
    '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
} 

And then:

<a href="javascript:del_cookie(name);">KILL</a>

Kill the roundcube_sessauth cookie?

Charlie
  • 11,380
  • 19
  • 83
  • 138
  • 1
    `name`? What's the point of that? Or is it a hangover from a more versatile version that allows you to specify the cookie name? – paxdiablo May 15 '12 at 01:21
  • Don't think that's a dupe. It specifically asks about clearing _all_ cookies. – paxdiablo May 15 '12 at 01:22
  • 1
    I don't want to delete all cookies... Just one. I found the basic code from googling around. And of course I tried it. @Paxdiablo I assumed it for naming the cookie, but I might be completely wrong. – Charlie May 15 '12 at 01:22
  • @paxdiablo - `How can I delete a ` **`specific`** `cookie`... – Derek 朕會功夫 May 15 '12 at 04:02
  • 1
    @Derek et al, you seem to misunderstand. I was stating that the proposed dupe from Gabe wasn't a dupe at all because the proposed dupe asked about deleting _all_ cookies and this question asks about deleting a _specific_ cookie. I was _not_ complaining about this question in any way, shape or form :-) – paxdiablo May 15 '12 at 04:10
  • @paxdiablo - Oh, didn't read the whole thing... Sorry! – Derek 朕會功夫 May 15 '12 at 04:30
  • possible duplicate of [javascript - delete cookie](http://stackoverflow.com/questions/2144386/javascript-delete-cookie) – Dave Jarvis Mar 27 '14 at 23:31

8 Answers8

354

You should define the path on which the cookie exists to ensure that you are deleting the correct cookie.

function set_cookie(name, value) {
  document.cookie = name +'='+ value +'; Path=/;';
}
function delete_cookie(name) {
  document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}

If you don't specify the path, the browser will set a cookie relative to the page you are currently on, so if you delete the cookie while on a different page, the other cookie continues its existence.

Edit based on @Evan Morrison's comment. Be aware that in some cases to identify the correct cookie, the Domain parameter is required. Usually it's defined as Domain=.yourdomain.example. Placing a dot in front of your domain name means that this cookie may exist on any sub-domain (www also counts as sub-domain).

Also, as mentioned in @RobertT's answer, HttpOnly cookies cannot be deleted with JavaScript on the client side.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
emii
  • 3,794
  • 1
  • 12
  • 13
  • 30
    This was driving me nuts! After I added Path=/, I was able to delete. Thanks! – duyn9uyen Sep 12 '14 at 18:05
  • 13
    This should be the correct answer, it doesn't work without `Path` in most cases. – SuperMarco Jun 09 '15 at 13:36
  • 14
    I wasn't able to delete a cookie until I added both path and the correct 'Domain=value; ' ... So my statement was: document.cookie = "cookieName= ; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT; domain=.myDomain.com" – Evan Morrison May 19 '16 at 00:01
  • 1
    This solution still works, but can be written slightly more concisely with `Max-Age=0`, if you care about such things. – MaxPRafferty Nov 06 '17 at 21:05
201

In order to delete a cookie set the expires date to something in the past. A function that does this would be.

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

Then to delete a cookie named roundcube_sessauth just do.

delete_cookie('roundcube_sessauth');
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
11

//if passed exMins=0 it will delete as soon as it creates it.

function setCookie(cname, cvalue, exMins) {
    var d = new Date();
    d.setTime(d.getTime() + (exMins*60*1000));
    var expires = "expires="+d.toUTCString();  
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

setCookie('cookieNameToDelete','',0) // this will delete the cookie.
Kishor Patil
  • 900
  • 7
  • 9
9

I'm not really sure if that was the situation with Roundcube version from May '12, but for current one the answer is that you can't delete roundcube_sessauth cookie from JavaScript, as it is marked as HttpOnly. And this means it's not accessible from JS client side code and can be removed only by server side script or by direct user action (via some browser mechanics like integrated debugger or some plugin).

RobertT
  • 4,300
  • 3
  • 29
  • 36
2

You can try this solution

var d = new Date();
d.setTime(d.getTime());
var expires = "expires="+d.toUTCString();
document.cookie = 'COOKIE_NAME' + "=" + "" + ";domain=domain.example;path=/;" + expires;
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Nafees
  • 21
  • 5
1
function deleteCookieByName(name) {
    let newCookie = document.cookie.replace(new RegExp(`${name}=[^ ]*( )?`), '');
    document.__defineGetter__("cookie", function() {return newCookie} );
}
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
itzhar
  • 12,743
  • 6
  • 56
  • 63
  • 2
    Please read [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). While this code block may answer the OP's question, this answer would be much more useful if you explain how this code is different from the code in the question, what you've changed, why you've changed it and why that solves the problem without introducing others. – Saeed Zhiany Jun 20 '22 at 03:00
0

In my case I used blow code for different environment.

  document.cookie = name +`=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;Domain=.${document.domain.split('.').splice(1).join('.')}`;
Amir Movahedi
  • 1,802
  • 3
  • 29
  • 52
0

for Cookies you can refer this cookie documentation

for setting the cookies with domain in js:

 function setCrossSubdomainCookie(cookieName, cookieValue, minutes) {
  const assign = `${name}=${escape(value)};`
  const d = new Date()
  d.setTime(d.getTime() + minutes * 60 * 1000)
  const expires = `expires=${d.toUTCString()};`
  const path = "path=/;"
  const domain = "domain=.domainName.example;"
  document.cookie = assign + expires + path + domain
}

For deleting the cookies with domain in js:

export async function deleteCookie(name) {
  document.cookie = `${name}=; path=/; domain=.edyst.com; expires=${new Date(
    0
  ).toUTCString()}`
}

Note: we cannot store cookies without expiry time but domain is optional can be excluded and if want to store cookie for long or don't want your cookie to expire then use this while setting the cookie

 const expires = "expires=Fri, 31 Dec 9999 23:59:59 GMT"
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Tarun Jain
  • 411
  • 5
  • 17