clearCookies
Clear browser cookies for a domain.
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 specific cookies inside a single test or test isolation is disabled.
Syntax​
cy.clearCookies()
cy.clearCookies(options)
Usage​
Correct Usage
cy.clearCookies() // Clear cookies for the currrent domain
Arguments​
options (Object)
Pass in an options object to change the default behavior of cy.clearCookies().
| Option | Default | Description |
|---|---|---|
domain | Hostname of the application under test | Clears the cookies from the specified domain |
log | true | Displays the command in the Command log |
timeout | responseTimeout | Time to wait for cy.clearCookies() to resolve before timing out |
Yields ​
cy.clearCookies()yieldsnull.
Examples​
No Args​
Clear cookies after logging in End-to-End Only​
In this example, your server sends back a session cookie on login.
cy.clearCookies() 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.clearCookies()
cy.visit('/dashboard') // redirected back to login
cy.url().should('include', 'login')
Notes​
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.clearCookies()requires being chained off ofcy.
Assertions ​
cy.clearCookies()yieldsnull, so there's nothing to assert on. To confirm the cookies are cleared, assert oncy.getCookies()afterward, for examplecy.getCookies().should('be.empty').
Timeouts ​
cy.clearCookies()can time out waiting for the browser to clear the cookies. It waits up to theresponseTimeout(or thetimeoutoption, when passed) before failing.- If the application under test is on a different origin,
cy.clearCookies()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​
Clear cookies after getting cookies
cy.getCookies().should('have.length', 1)
cy.clearCookies()
cy.getCookies().should('be.empty')
The commands above display in the Command Log as:

When clicking on clearCookies within the command log, the console outputs the
following:
