12

In my app there are facebook and twitter login using browser and after login it stores cookies automatically. i have to logout facebook that will be happen to remove facebook cookies value but i don't know how to remove particular cookies.

if i remove all cookies using:

CookieManager cm = CookieManager.getInstance(this);
cm.removeAllCookies();

but it removes all cookies value means it will logout both facebook and twitter both.

my question is -- how to remove particular cookie value.

thanks..

Simley Jo
  • 33
  • 8
Archana
  • 509
  • 2
  • 6
  • 20
  • http://stackoverflow.com/questions/20940012/android-cookiemanager-setcookie-creates-multiple-cookies – trante Jan 07 '14 at 22:35

1 Answers1

12

You should use CookieManager.setCookie() and set the cookie to the empty string. Something like this should work:

String cookieString = "cookieName=''";
cookieManager.setCookie(cookieDomain, cookieString);

In addition to setting the cookie value to empty, you can also expire the cookie by setting the 'expire' value in the cookie string to a time in the past. For example:

String cookieString = "cookieName=;expires=Mon, 17 Oct 2011 10:47:11 UTC;";
spatulamania
  • 6,613
  • 2
  • 30
  • 26
  • 4
    The part of the answer which states that setting expiry value to a time in the past does not work. This is clearly mentioned in the javadoc [link](http://developer.android.com/reference/android/webkit/CookieManager.html#setCookie(java.lang.String, java.lang.String))) "The cookie being set will be ignored if it is expired." I also verified the same in Android 4.2.1 device. – garnet Apr 19 '15 at 06:46
  • 1
    tried this and seems to be not working. Also digged the code and there seems to be no removal when we enter empty value – andinrajesh Oct 16 '18 at 13:26