clearLocalStorage

Improve this doc

Clear data in local storage for current domain and subdomain.

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 local storage.

options (Object)

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

Option Default Description
log true Displays the command in the Command log

Yields

  • cy.clearLocalStorage() yields null.

  • cy.clearLocalStorage() cannot be chained further.

Examples

No Args

Clear all local storage

cy.clearLocalStorage()

Specific Key

Clear local storage with key ‘appName’

cy.clearLocalStorage('appName')

Clear all local storage matching /app-/

cy.clearLocalStorage(/app-/)

Rules

Requirements

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

Assertions

  • cy.clearLocalStorage() cannot have any assertions chained.

Timeouts

  • cy.clearLocalStorage() can not 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