Skip to main content
Cypress App

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 Learn about subject management​

  • 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 Learn about chaining commands​

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

Assertions Learn about assertions​

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

Timeouts Learn about timeouts​

  • cy.clearLocalStorage() cannot time out.

Command Log​

Clear all localStorage

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​