How To Delete Specific/All Cookies In Selenium WebDriver Test

Earlier we learnt how to extract all cookies of current domain In THIS POST and add new cookie for that specific domain In previous post In selenium WebDriver test. Cookies plays very Important role In any website and you must know how to add new cookie or delete specific or all cookies If you
are test engineer. Now supposing you have a test scenario where you have to delete specific cookie or you have to delete all cookies for that domain.

Manually you can do It very easily. Same way, selenium WebDriver has also cookie class which contains function deleteCookieNamed() to delete specific cookie and .deleteAllCookies(); to delete all cookies of domain under test.

Syntax for deleting specific cookie Is as bellow.
where  testCookie2 Is name of cookie.

Syntax for deleting all cookie Is as bellow.

Bellow given example will show you how to delete specific cookie by Its name or delete all cookies.

package Testing_Pack;

import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class deleteCookies {
WebDriver driver;
 public void setup() throws Exception {
  driver = new FirefoxDriver();
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  driver.get("your website name");
 public void deleteCookie(){
  //Add 2 test cookies for your domain.
  Cookie cookie1 = new Cookie("testCookie1", "WSfed-ffsd-234DFGe-YUTYU");
  Cookie cookie2 = new Cookie("testCookie2", "xxCDFSS-234DDs-423SS2-34EE");
  //Get and print all cookies for google domain after adding cookies manually.
  Set<Cookie> totalCookies1 = driver.manage().getCookies();
  System.out.println("Total Number Of cookies : " +totalCookies1.size());
  for (Cookie currentCookie : totalCookies1) {
      System.out.println(String.format("%s -> %s -> %s", "Domain Name : "+currentCookie.getDomain(), "Cookie Name : "+currentCookie.getName(), "Cookie Value : "+currentCookie.getValue()));
  //Deleting specific testCookie2 cookie.
  //Get and print all cookies for google domain after deleting cookie manually.
  Set<Cookie> totalCookies2 = driver.manage().getCookies();
  System.out.println("Total Number Of cookies after deleting one cookie : " +totalCookies2.size());
  for (Cookie currentCookie : totalCookies2) {
      System.out.println(String.format("%s -> %s -> %s", "Domain Name : "+currentCookie.getDomain(), "Cookie Name : "+currentCookie.getName(), "Cookie Value : "+currentCookie.getValue()));
  //Deleting all cookies.
  //Print number of cookies after deleting all cookies.
  Set<Cookie> totalCookies3 = driver.manage().getCookies();
  System.out.println("Total Number Of cookies after delete all cookies : " +totalCookies3.size());

Above example will add two test cookies Initially then It will delete specific cookie and at last It will delete all cookies. This Is the way to play with cookies In selenium WebDriver.