Skip to main content
Cypress App

clearAllSessionStorage

Clear sessionStorage data for all origins with which the test has interacted.

caution

Cypress automatically clears all session storage 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 sessionStorage inside a single test or test isolation is disabled.

Syntax​

cy.clearAllSessionStorage()
cy.clearAllSessionStorage(options)

Usage​

Correct Usage

cy.clearAllSessionStorage()

Arguments​

options (Object)

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

OptionDefaultDescription
logtrueDisplays the command in the Command Log

Yields ​

  • cy.clearAllSessionStorage() yields null.

Examples​

No Args​

Clear session storage for every origin
End-to-End Only
​

This clears sessionStorage on both origins, including the one visited inside cy.origin().

cy.visit('/')
cy.window().then((win) => win.sessionStorage.setItem('draft', 'home'))
cy.origin('https://example.cypress.io', () => {
cy.visit('/')
cy.window().then((win) => win.sessionStorage.setItem('draft', 'docs'))
})
cy.getAllSessionStorage().should('not.be.empty')
cy.clearAllSessionStorage()
cy.getAllSessionStorage().should('be.empty')

Options​

Clear session storage without logging to the Command Log​

Pass { log: false } to keep cy.clearAllSessionStorage() out of the Command Log, which is useful when it runs in a hook before every test and would otherwise repeat in every test's log.

cy.clearAllSessionStorage({ log: false })

Common workflows​

Reset session storage between tests with test isolation off
End-to-End Only
​

With test isolation off, clear session storage in a beforeEach() hook so each test starts from the same state. Pair it with cy.clearAllCookies() and cy.clearAllLocalStorage() for a full reset.

describe('checkout', { testIsolation: false }, () => {
beforeEach(() => {
cy.clearAllCookies()
cy.clearAllLocalStorage()
cy.clearAllSessionStorage()
})
})

Restart a multi-step form saved in session storage
End-to-End Only
​

Apps often keep a multi-step form's progress in sessionStorage so a reload doesn't lose the user's place. Clear it partway through a test to check that the form starts over when the saved progress is gone.

cy.visit('/signup')
cy.get('[data-cy="email"]').type('[email protected]')
cy.get('[data-cy="next-step"]').click()

cy.clearAllSessionStorage()
cy.reload()
cy.get('[data-cy="email"]').should('have.value', '')

Clear session storage on the current origin only​

To clear sessionStorage on the current origin and leave other origins in place, call sessionStorage.clear() on the window from cy.window().

cy.window().then((win) => win.sessionStorage.clear())

Notes​

Origins cleared by cy.clearAllSessionStorage()​

Cypress tracks every origin that serves an HTML page, and cy.clearAllSessionStorage() clears sessionStorage for each of them. When test isolation is enabled, Cypress resets that list at the start of each test. When it's off, the list carries over between the tests in a spec file, so the command also clears origins that earlier tests in the spec loaded.

HTTP origins in an HTTPS test​

When your app runs on HTTPS, cy.clearAllSessionStorage() clears only the HTTPS origins and leaves sessionStorage on any HTTP origins in place. Browsers block an HTTPS page from reaching an HTTP origin's storage. If every origin your test visits uses the same protocol, this doesn't affect you.

Origins that don't respond within 2 seconds​

Cypress clears storage on other origins through a hidden iframe for each one. If an origin doesn't respond within 2 seconds, Cypress logs a warning in the Command Log and moves on instead of failing the test.

Rules​

Requirements ​

  • cy.clearAllSessionStorage() requires being chained off of cy.

Assertions ​

  • cy.clearAllSessionStorage() yields null, so there's nothing to assert on. To confirm session storage is cleared, assert on cy.getAllSessionStorage() afterward, for example cy.getAllSessionStorage().should('be.empty').

Timeouts ​

Command Log​

Clear session storage on two origins

cy.visit('/')
cy.window().then((win) => win.sessionStorage.setItem('draft', 'home'))
cy.origin('https://example.cypress.io', () => {
cy.visit('/')
cy.window().then((win) => win.sessionStorage.setItem('draft', 'docs'))
})
cy.getAllSessionStorage().should('not.be.empty')
cy.clearAllSessionStorage()
cy.getAllSessionStorage().should('be.empty')

The commands above display in the Command Log as:

Passing test in the Command Log: session storage on two origins, then clearAllSessionStorage, then expected {} to be empty

History​

VersionChanges
12.0.0cy.clearAllSessionStorage() command added

See also​