Skip to main content

clearLocalStorage

Clear data in localStorage for current domain and subdomain.

caution

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.clearLocalStorage()
cy.clearLocalStorage(key)
cy.clearLocalStorage(options)
cy.clearLocalStorage(keys, options)

Usage​

Correct Usage

cy.clearLocalStorage() // clear all local storage

Arguments​

keys (String, RegExp)

Specify key to be cleared in localStorage.

options (Object)

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

OptionDefaultDescription
logtrueDisplays the command in the Command log

Yields ​

  • cy.clearLocalStorage() yields the localStorage for the current domain.

Examples​

No Args​

Clear all localStorage​

cy.clearLocalStorage()

Specific Key​

Clear localStorage with the key 'appName'​

cy.clearLocalStorage('appName')

Clear all localStorage matching /app-/ RegExp​

cy.clearLocalStorage(/app-/)

Rules​

Requirements ​

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

Assertions ​

  • cy.clearLocalStorage() will only run assertions you have chained once, and will not retry.

Timeouts ​

  • cy.clearLocalStorage() cannot time out.

Command Log​

cy.clearLocalStorage(/prop1|2/).then((ls) => {
expect(ls.getItem('prop1')).to.be.null
expect(ls.getItem('prop2')).to.be.null
expect(ls.getItem('prop3')).to.eq('magenta')
})

The commands above will display in the Command Log as:

Command log for clearLocalStorage

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

console.log for clearLocalStorage

See also​