---
id: api/commands/clearcookie
title: cy.clearCookie()
description: Clear a specific browser cookie in Cypress.
section: api
source_path: docs/api/commands/clearcookie.mdx
version: f60251bf707b0077a9535579c9f90d615440cc35
updated_at: '2026-10-16T10:56:58.670Z'
---
# clearCookie

Clear a specific browser cookie.

Cypress automatically clears all cookies _before_ each test to prevent state from being shared across tests when [test isolation](/llm/markdown/app/core-concepts/writing-and-organizing-tests.md#Test-Isolation) is enabled. You shouldn't need to use this command unless you're using it to clear a specific cookie inside a single test or test isolation is disabled.

## Syntax

```
cy.clearCookie(name)
cy.clearCookie(name, options)
```

### Usage

**Correct Usage**

```
cy.clearCookie('authId') // clear the 'authId' cookie
```

**Incorrect Usage**

```
cy.clearCookie() // Errors, a cookie name is required
cy.clearCookie({ domain: 'example.com' }) // Errors, name must be a string
```

### Arguments

**name _(String)_**

The name of the cookie to clear. Required, and must be a string.

**options _(Object)_**

Pass in an options object to change the default behavior of `cy.clearCookie()`.

| Option | Default | Description |
| --- | --- | --- |
| `domain` | Hostname of the application under test | Clears the cookie from the specified domain |
| `log` | `true` | Displays the command in the [Command log](/llm/markdown/app/core-concepts/open-mode.md#Command-Log) |
| `timeout` | [`responseTimeout`](/llm/markdown/app/references/configuration.md#Timeouts) | Time to wait for `cy.clearCookie()` to resolve before [timing out](#Timeouts) |

### Yields

*   `cy.clearCookie()` yields `null`.

## Examples

### Name

#### Clear a cookie after logging in[

End-to-End Only

](/llm/markdown/app/core-concepts/testing-types.md#What-is-E2E-Testing)

In this example, your server sends back a session cookie on login. `cy.clearCookie('session_id')` removes that cookie, so when you visit a page that requires authentication, the server redirects you back to the login page.

```
// assume the user just logged in
cy.contains('Login').click()
cy.url().should('include', 'profile')
cy.clearCookie('session_id')
cy.visit('/dashboard') // redirected back to login
cy.url().should('include', 'login')
```

#### Clear an HttpOnly session cookie and check the API[

End-to-End Only

](/llm/markdown/app/core-concepts/testing-types.md#What-is-E2E-Testing)

Your application's own JavaScript can't read or remove an `HttpOnly` cookie through `document.cookie`. `cy.clearCookie()` clears cookies through the browser's automation layer, so it removes `HttpOnly` cookies too. Clear the session cookie, then confirm the API rejects the next request.

```
// session_id is HttpOnly, so document.cookie can't remove it
cy.clearCookie('session_id')
cy.request({ url: '/api/profile', failOnStatusCode: false })
  .its('status')
  .should('eq', 401)
```

#### Show a cookie consent banner again[

End-to-End Only

](/llm/markdown/app/core-concepts/testing-types.md#What-is-E2E-Testing)

Clear a single cookie and reload to test a first-visit flow, such as a cookie consent banner, without logging out. Every other cookie stays in place.

```
cy.clearCookie('cookie_consent')
cy.reload()
cy.get('[data-cy="consent-banner"]').should('be.visible')
```

#### Clear a cookie set during a cy.origin() login[

End-to-End Only

](/llm/markdown/app/core-concepts/testing-types.md#What-is-E2E-Testing)

When you log in through another origin with [`cy.origin()`](/llm/markdown/api/commands/origin.md), the identity provider often redirects back to your application, which then sets its own session cookie. Once `cy.origin()` returns, `cy.clearCookie()` clears that cookie from the application under test.

```
cy.visit('/')
cy.get('[data-cy="login"]').click()
cy.origin('https://auth.example.com', () => {
  cy.get('[data-cy="username"]').type('user')
  cy.get('[data-cy="password"]').type('*****')
  cy.get('[data-cy="submit"]').click()
})
cy.clearCookie('user')
cy.reload() // the app shows the logged-out state
```

#### Clear one cookie before each test with test isolation off[

End-to-End Only

](/llm/markdown/app/core-concepts/testing-types.md#What-is-E2E-Testing)

With [test isolation](/llm/markdown/app/core-concepts/test-isolation.md) turned off, cookies from one test carry over to the next. Call `cy.clearCookie()` in a `beforeEach` hook to reset only the cookie you care about while keeping the rest, such as a login session you want to reuse across the suite.

```
describe('Onboarding tour', { testIsolation: false }, () => {
  before(() => {
    // log in once; the session cookie carries over between tests
    cy.visit('/login')
    cy.get('[data-cy="username"]').type('user')
    cy.get('[data-cy="password"]').type('*****')
    cy.get('[data-cy="submit"]').click()
    cy.url().should('include', '/dashboard') // wait for the login to finish
  })

  beforeEach(() => {
    // keep the session cookie, but start each test before the tour is dismissed
    cy.clearCookie('tour_dismissed')
  })

  it('shows the tour on the dashboard', () => {
    cy.visit('/dashboard')
    cy.get('[data-cy="tour"]').should('be.visible')
  })
})
```

### Name and options

#### Clear a cookie on a specific domain[

End-to-End Only

](/llm/markdown/app/core-concepts/testing-types.md#What-is-E2E-Testing)

By default, `cy.clearCookie()` clears the cookie from the hostname of the application under test. Pass `domain` to clear a cookie set on a different domain.

```
cy.clearCookie('session_id', { domain: 'auth.example.com' })
cy.getCookie('session_id', { domain: 'auth.example.com' }).should('be.null')
```

## Notes

### Clearing a cookie that doesn't exist

`cy.clearCookie()` doesn't fail when no cookie matches the name. It still yields `null`, and the console output for the command notes that no cookie with that name was found or removed.

### Default cookie domain before visiting a page

The `domain` option defaults to the hostname of the application under test. Before [`cy.visit()`](/llm/markdown/api/commands/visit.md) loads a page, the application under test is on `about:blank`, so Cypress uses the hostname it serves the spec from instead (usually `localhost`). Pass `domain` explicitly to work with cookies on another domain before visiting.

## Rules

### Requirements

*   `cy.clearCookie()` requires being chained off of `cy`.

### Assertions

*   `cy.clearCookie()` yields `null`, so there's nothing to assert on. To confirm the cookie is cleared, assert on [`cy.getCookie()`](/llm/markdown/api/commands/getcookie.md) afterward, for example `cy.getCookie('session_id').should('be.null')`.

### Timeouts

*   `cy.clearCookie()` can time out waiting for the browser to clear the cookie. It waits up to the [`responseTimeout`](/llm/markdown/app/references/configuration.md#Timeouts) (or the `timeout` option, when passed) before failing.
*   If the application under test is on a different origin, `cy.clearCookie()` retries for up to the [`defaultCommandTimeout`](/llm/markdown/app/references/configuration.md#Timeouts) (or the `timeout` option, when passed) and then fails with a cross-origin error. Run it inside [`cy.origin()`](/llm/markdown/api/commands/origin.md) to work with cookies on that origin.

## Command Log

**_Clearing a cookie after setting a cookie_**

```
cy.setCookie('foo', 'bar')
cy.clearCookie('foo')
cy.getCookie('foo').should('be.null')
```

The commands above display in the Command Log as:

When you click `clearCookie` in the Command Log, the console outputs the following:

## History

| Version | Changes |
| --- | --- |
| [11.0.0](/llm/markdown/app/references/changelog.md#11-0-0) | Added `domain` option. |
| [0.6.8](/llm/markdown/app/references/changelog.md#0-6-8) | `cy.clearCookie()` command added. |

## See also

*   [`cy.clearCookies()`](/llm/markdown/api/commands/clearcookies.md)
*   [`cy.clearLocalStorage()`](/llm/markdown/api/commands/clearlocalstorage.md)
*   [`cy.getCookies()`](/llm/markdown/api/commands/getcookies.md)
*   [`cy.getCookie()`](/llm/markdown/api/commands/getcookie.md)
*   [`cy.setCookie()`](/llm/markdown/api/commands/setcookie.md)
*   [`Cypress.Cookies.debug()`](/llm/markdown/api/cypress-api/cookies.md)
