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 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 |
timeout | responseTimeout | Time to wait for cy.clearCookie() to resolve before timing out |
Yields ​
cy.clearCookie()yieldsnull.
Examples​
Name​
Clear a cookie after logging in End-to-End Only​
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​
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​
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​
When you log in through another origin with cy.origin(),
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​
With test isolation 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​
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() 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 ofcy.
Assertions ​
cy.clearCookie()yieldsnull, so there's nothing to assert on. To confirm the cookie is cleared, assert oncy.getCookie()afterward, for examplecy.getCookie('session_id').should('be.null').
Timeouts ​
cy.clearCookie()can time out waiting for the browser to clear the cookie. It waits up to theresponseTimeout(or thetimeoutoption, when passed) before failing.- If the application under test is on a different origin,
cy.clearCookie()retries for up to thedefaultCommandTimeout(or thetimeoutoption, when passed) and then fails with a cross-origin error. Run it insidecy.origin()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 | Added domain option. |
| 0.6.8 | cy.clearCookie() command added. |