Skip to main content

clearLocalStorage

Clear data in localStorage for current domain and subdomain.

caution

Cypress automatically runs this command before each test to prevent state from being shared across tests. You shouldn't need to use this command unless you're using it to clear localStorage inside a single test.

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 `null`.
  • `cy.clearLocalStorage()` cannot be chained further.

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()` cannot have any assertions chained.

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