Deleting Cookies

Introduction

Deleting Cookies in Selenium allows you to remove one or all cookies stored in the browser during an automation session. Cookies store important information such as user sessions, login authentication, preferences, and website-specific settings. Removing cookies helps simulate a fresh browser session and ensures that previous test data does not affect subsequent test cases.

Selenium provides two methods for deleting cookies:

  • delete_cookie(name) – Deletes a specific cookie by its name.

  • delete_all_cookies() – Deletes every cookie stored for the current domain.

Deleting cookies is widely used in automation testing to verify login/logout functionality, session expiration, user isolation, and clean browser state before test execution.

In this tutorial, you will learn what deleting cookies means, why it is used, Selenium cookie deletion methods, practical examples, real-world applications, common mistakes, best practices, and frequently asked interview questions.


What is Deleting Cookies?

Deleting cookies means removing cookies that are currently stored in the browser.

Selenium can:

  • Delete one specific cookie.

  • Delete every cookie stored for the current website.

Once a cookie is deleted, the browser no longer sends that cookie with future requests.


Why Do We Delete Cookies?

Deleting cookies is commonly used to:

  • Clear user sessions.

  • Simulate first-time visitors.

  • Test logout functionality.

  • Remove authentication cookies.

  • Prevent test data contamination.

  • Start each test with a clean browser session.


Selenium Methods for Deleting Cookies

MethodDescription
delete_cookie(name)Deletes one specific cookie by name.
delete_all_cookies()Deletes every cookie stored for the current website.

Syntax

Delete a Specific Cookie

driver.delete_cookie("cookie_name")

Delete All Cookies

driver.delete_all_cookies()

Practical Example

The following example demonstrates how to delete both a specific cookie and all cookies.

The automation script opens the Selenium practice website, creates two cookies, deletes one cookie by name, verifies that it has been removed, then deletes all remaining cookies and confirms that the browser no longer contains any cookies.

from selenium import webdriver


# Topic: 9. Managing Browser Sessions - Deleting Cookies
# Practice site: https://the-internet.herokuapp.com/
#
# delete_cookie() removes one cookie by name and delete_all_cookies() clears
# every cookie in the current session.


def test_delete_cookies():
    driver = webdriver.Chrome()

    try:
        driver.get("https://the-internet.herokuapp.com/")

        driver.add_cookie(
            {
                "name": "temp_cookie",
                "value": "to_delete",
            }
        )

        driver.add_cookie(
            {
                "name": "keep_cookie",
                "value": "keep",
            }
        )

        driver.delete_cookie("temp_cookie")

        assert driver.get_cookie("temp_cookie") is None

        driver.delete_all_cookies()

        assert driver.get_cookies() == []

    finally:
        driver.quit()

Output

Chrome launched successfully.

Website opened successfully.

Two cookies added.

Deleted cookie:
temp_cookie

Verified cookie no longer exists.

Deleted all remaining cookies.

No cookies found.

Assertion Passed.

Test Executed Successfully.

Understanding the Code

Import WebDriver

from selenium import webdriver

Imports the Selenium WebDriver module.


Launch Chrome

driver = webdriver.Chrome()

Starts a new Chrome browser session.


Open the Website

driver.get("https://the-internet.herokuapp.com/")

Navigates to the Selenium practice website.


Add Sample Cookies

driver.add_cookie(
    {
        "name": "temp_cookie",
        "value": "to_delete",
    }
)

driver.add_cookie(
    {
        "name": "keep_cookie",
        "value": "keep",
    }
)

Creates two cookies for demonstration purposes.


Delete a Specific Cookie

driver.delete_cookie("temp_cookie")

Deletes only the cookie named temp_cookie.


Verify Cookie Deletion

assert driver.get_cookie("temp_cookie") is None

Confirms that the specified cookie has been successfully removed.


Delete All Cookies

driver.delete_all_cookies()

Deletes every remaining cookie stored for the current website.


Verify All Cookies Were Deleted

assert driver.get_cookies() == []

Verifies that no cookies remain in the browser.


Close the Browser

driver.quit()

Closes all browser windows and ends the WebDriver session.


Browser Execution Flow

Launch Chrome
      │
      ▼
Open Website
      │
      ▼
Add Sample Cookies
      │
      ▼
Delete Specific Cookie
      │
      ▼
Verify Cookie Removal
      │
      ▼
Delete All Cookies
      │
      ▼
Verify No Cookies Exist
      │
      ▼
Assertions Pass
      │
      ▼
Close Browser

Cookie Deletion Methods

MethodDeletes
delete_cookie("name")One specific cookie
delete_all_cookies()Every cookie in the current browser session

Automation Testing Example

Suppose an online shopping application allows users to remain logged in using session cookies.

Before testing the login page:

  • Selenium deletes all cookies.

  • The browser behaves like a first-time visitor.

  • The login page is displayed correctly.

  • Previous authentication sessions do not affect the test.

This ensures reliable and repeatable test execution.


Real-World Example

Consider an online banking application.

During logout testing:

  • Selenium logs into the application.

  • Deletes the authentication cookie.

  • Refreshes the page.

  • Verifies that the user is redirected to the login screen.

This confirms that removing the session cookie successfully ends the authenticated session.


Common Mistakes Beginners Make

Trying to Delete Cookies Before Opening a Website

Incorrect

driver = webdriver.Chrome()

driver.delete_all_cookies()

No website has been loaded, so there are no cookies associated with a domain.


Correct

driver.get("https://the-internet.herokuapp.com/")

driver.delete_all_cookies()

Deleting the Wrong Cookie Name

delete_cookie() only removes the cookie whose name exactly matches the provided value.

Always verify the cookie name before deleting it.


Forgetting to Verify Deletion

After deleting cookies, retrieve them using get_cookie() or get_cookies() to ensure the deletion was successful.


Best Practices

  • Open the target website before deleting cookies.

  • Use delete_cookie() when removing a specific cookie.

  • Use delete_all_cookies() before tests that require a clean browser session.

  • Verify cookie deletion after performing the operation.

  • Clear cookies between independent test cases to avoid session contamination.

  • Always close the browser using driver.quit().


Conclusion

Deleting Cookies allows Selenium to remove browser cookies and create a clean testing environment. Using delete_cookie() and delete_all_cookies(), automation engineers can simulate new users, validate logout functionality, clear authentication sessions, and ensure that previous test data does not interfere with future tests.

Proper cookie deletion is an important part of browser session management and reliable automation testing.


Frequently Asked Questions (FAQs)

What is delete_cookie() in Selenium?

delete_cookie(name) removes one specific cookie from the current browser session.


What is delete_all_cookies()?

delete_all_cookies() removes every cookie stored for the current website.


How do I delete a specific cookie?

driver.delete_cookie("temp_cookie")

How do I delete all cookies?

driver.delete_all_cookies()

Why are cookies deleted during automation?

Cookies are deleted to clear browser sessions, simulate first-time users, test logout functionality, and ensure clean and independent test execution.


Key Takeaways

  • Selenium provides delete_cookie() to remove a specific cookie.

  • Use delete_all_cookies() to clear every cookie from the current browser session.

  • Open the target website before deleting cookies.

  • Verify cookie deletion using get_cookie() or get_cookies().

  • Cookie deletion helps create a clean browser state for reliable testing.

  • Clearing cookies is useful for testing login, logout, and session management.

  • Always close the browser using driver.quit().

  • Deleting Cookies is an essential Selenium browser session management concept and a frequently asked interview topic.