Skip to main content
Cypress App

clearCookie

Clear a specific browser cookie.

caution

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().

OptionDefaultDescription
domainHostname of the application under testClears the cookie from the specified domain
logtrueDisplays the command in the Command log
timeoutresponseTimeoutTime to wait for cy.clearCookie() to resolve before timing out

Yields ​

  • cy.clearCookie() yields null.

Examples​

Name​

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')

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)

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')

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

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​

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​

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.

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 of cy.

Assertions ​

  • cy.clearCookie() yields null, so there's nothing to assert on. To confirm the cookie is cleared, assert on cy.getCookie() 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 (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 (or the timeout option, when passed) and then fails with a cross-origin error. Run it inside cy.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:

Command Log showing setCookie, clearCookie, and getCookie commands for the foo cookie

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

Console output for clearCookie showing the yielded null and the cleared cookie

History​

VersionChanges
11.0.0Added domain option.
0.6.8cy.clearCookie() command added.

See also​