clearAllLocalStorage
Clear
localStorage
data for all origins with which the test has interacted.
Cypress automatically clears all local 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 localStorage inside a single test or test isolation is disabled.
Syntax​
cy.clearAllLocalStorage()
cy.clearAllLocalStorage(options)
Usage​
Correct Usage
cy.clearAllLocalStorage()
Arguments​
options (Object)
Pass in an options object to change the default behavior of
cy.clearAllLocalStorage().
| Option | Default | Description |
|---|---|---|
log | true | Displays the command in the Command Log |
Yields ​
cy.clearAllLocalStorage()yieldsnull.
Examples​
No Args​
Clear local storage for every origin End-to-End Only​
cy.clearAllLocalStorage() clears localStorage on every origin the test has
loaded a page from, including origins visited inside
cy.origin().
cy.visit('https://example.cypress.io')
cy.window().then((win) => win.localStorage.setItem('theme', 'dark'))
cy.origin('https://www.cypress-dx.com', () => {
cy.visit('/')
cy.window().then((win) => win.localStorage.setItem('theme', 'light'))
})
cy.getAllLocalStorage().should('not.be.empty')
cy.clearAllLocalStorage()
cy.getAllLocalStorage().should('be.empty')
Options​
Clear local storage without logging to the Command Log​
Pass { log: false } to keep cy.clearAllLocalStorage() 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.clearAllLocalStorage({ log: false })
Common workflows​
Reset local storage between tests with test isolation off End-to-End Only​
When
test isolation
is off, localStorage carries over from one test to the next. Clear it in a
beforeEach() hook so each test starts from the same state. Pair it with
cy.clearAllCookies() and
cy.clearAllSessionStorage() for a full
reset.
describe('checkout', { testIsolation: false }, () => {
beforeEach(() => {
cy.clearAllCookies()
cy.clearAllLocalStorage()
cy.clearAllSessionStorage()
})
})
Forget a remembered user on a sign-in origin End-to-End Only​
When your app signs users in on a separate origin, that sign-in page can
remember the user in its own localStorage.
cy.clearLocalStorage() reaches only the
current origin, so use cy.clearAllLocalStorage() to forget the user on every
origin and test the signed-out experience.
cy.visit('/')
cy.origin('https://www.cypress-dx.com', () => {
cy.visit('/login')
cy.get('[data-cy="remember-me"]').check()
cy.get('[data-cy="login"]').click()
})
cy.clearAllLocalStorage()
cy.origin('https://www.cypress-dx.com', () => {
cy.visit('/login')
cy.get('[data-cy="email"]').should('have.value', '')
})
Choose between cy.clearLocalStorage() and cy.clearAllLocalStorage()​
cy.clearLocalStorage() clears the current
origin and can target specific keys. cy.clearAllLocalStorage() clears every
key on every tracked origin.
// Clear one key on the current origin
cy.clearLocalStorage('authToken')
// Clear every key on every tracked origin
cy.clearAllLocalStorage()
Notes​
Origins cleared by cy.clearAllLocalStorage()​
Cypress tracks every origin that serves an HTML page, and
cy.clearAllLocalStorage() clears localStorage 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.clearAllLocalStorage() clears only the HTTPS
origins and leaves localStorage 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.
Rules​
Requirements ​
cy.clearAllLocalStorage()requires being chained off ofcy.
Assertions ​
cy.clearAllLocalStorage()yieldsnull, so there's nothing to assert on. To confirm local storage is cleared, assert oncy.getAllLocalStorage()afterward, for examplecy.getAllLocalStorage().should('be.empty').
Timeouts ​
cy.clearAllLocalStorage()cannot time out. When an origin doesn't respond within 2 seconds, the command logs a warning and continues.
History​
| Version | Changes |
|---|---|
| 12.0.0 | cy.clearAllLocalStorage() command added |