16

Let's say I am at http://www.example.com and I want to delete a cookie whose domain is .example.com and another one whose domain is www.example.com.

I am currently using this generic function :

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

which only seems to be removing cookies whose domain is www.example.com.

But how can I specify so that it also removes cookies whose domain is .example.com ?

EDIT : Basically I'm looking for a function that can delete all cookies related to http://www.example.com as long as they don't have the httponly flag. Is there such a function?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
The Random Guy
  • 321
  • 2
  • 4
  • 10
  • possible duplicate of [Clearing all cookies with JavaScript](http://stackoverflow.com/questions/179355/clearing-all-cookies-with-javascript) – Álvaro González Jan 23 '14 at 12:34
  • Does this answer your question? [How to delete a cookie?](https://stackoverflow.com/questions/2144386/how-to-delete-a-cookie) – Liam May 10 '22 at 12:46

5 Answers5

18

You could do this only if you were at http://example.com and wanted to delete http://blah.example.com cookie. It wouldn't work from www.example.com either - only the "base" domain can delete subdomain cookies.

There are also "all-subdomain" cookies, which start with a ., and can also only be deleted by the base domain.

From the base domain, this should work to delete it:

document.cookie = 'my_cookie=; path=/; domain=.example.com; expires=' + new Date(0).toUTCString();

Or using the excellent jquery.cookie plugin:

$.cookie('my_cookie',null, {domain:'.example.com'})
Kevin
  • 4,225
  • 2
  • 37
  • 40
11

For security, you're not allowed to edit (or delete) a cookie on another site. Since there's no guarantee that you own both foo.domain.com and bar.domain.com, you won't be allowed to edit the cookies of foo.domain.com from bar.domain.com and vice versa.

Consider if you were allowed to do that and went to a malicious site, then back to your bank where you were about to deposit a cheque into your bank account. But while being on the malicious site, they updated your bank cookie with their own bank information. Now, suddenly, the cheque would be deposited into the malicious site's owner's bank account.

G.Ani
  • 93
  • 5
kba
  • 19,333
  • 5
  • 62
  • 89
  • Yes I can. I've just opened up my Chrome console on yahoo.com and I called the function of my original post which deleted a cookie set in www.yahoo.com. However I can't seem to be able to delete .yahoo.com cookies. I'm looking for a function that can do that by specifying the domain from which to delete the cookie. – The Random Guy Jul 28 '13 at 16:58
  • 2
    @TheRandomGuy The Chrome console isn't working under the same restrictions as the code executed from a website. On the website `foo.domain.com` you won't be able to edit the cookie on `bar.domain.com`. See [other](http://stackoverflow.com/questions/117240/is-it-possible-to-delete-subdomain-cookies) [similar](http://stackoverflow.com/questions/6525484/deleting-cookies-in-other-subdomains) [questions](http://stackoverflow.com/questions/3923285/how-to-remove-main-domain-cookie-from-sub-domain) if you aren't convinced. – kba Jul 28 '13 at 22:31
  • I am not looking at deleting cookies from a domain where my script will not be firing on. My script will be loaded on e.g. www.someurl.com and I want my script to delete the maximum number of cookies possible from www.someurl.com and .someurl.com – The Random Guy Jul 29 '13 at 18:39
  • 1
    @TheRandomGuy did you find a solution? – user3808307 Jun 11 '19 at 03:01
2

Just wanted to add to this reg. deleting top-level cookies from sub domains.

I was surfing "mysub.mysite.se" with the following script inside a referenced js-file (mysub.mysite.se/file.js).

This code did remove the _fbp cookie with domain ".mysite.se".

document.cookie = '_fbp=;expires=Thu, 01 Jan 2010 00:00:00 UTC; path=/; domain=.mysite.se';
document.cookie = '_fbp=;expires=Thu, 01 Jan 2010 00:00:00 UTC; path=/; domain=www.mysite.se';
document.cookie ='_fbp=;expires=Thu, 01 Jan 2010 00:00:00 UTC; path=/; domain=mysite.se';

So what @kba is saying is not 100% right, but this is the case when you try to remove a cookie on a 3rd party domain.

  • 1
    I also found it odd that there is no guarantee one own subdomain.domain.com even if they own .domain.com. Your code worked for me. I want to delete a top level cookie from a subdomain. Adding domain=.domain.com did the trick. – Francisco Luz Nov 24 '21 at 08:03
1

If you are using 'universal-cookie', you can just use remove() function check here.

cookies.remove('my_cookie', { domain: window.location.hostname}); - this domain

cookies.remove('my_cookie', { domain: '.example.com' }); - any other damain

Werthis
  • 557
  • 3
  • 15
-2

We need to set the cookie for delete them.

Example:

this.cookieService.set(cookiename, '', new Date('Thu, 01 Jan 1970 00:00:01 GMT'));

Please refer below Git location for more details. https://github.com/7leads/ngx-cookie-service/issues/5 https://github.com/angular/angular/issues/9954

user3198259
  • 178
  • 3
  • 13