How to Clear Cache between tests in Cypress

Related Product On This Page What does Cypress Cache include?June 18, 2026 · 7 min read · Tool Comparison

Related Product

How to Clear Cache between tests in Cypress

If you are act on creating web applications, you are using biscuit, local storage, and session storage. When you use JavaScript to make your web applications, you use all three mechanisms to store information on the client.

These means of storing information impact your testing methodology. No matter the type of testing that you are execute –,, penetration testing,, etc, you will need to take with these mechanisms. Your or must run on a clear machine to feign the unparalleled test instance they cater to.

Overview

Different Types of Cache Storage

  • Cookies
  • Session Storage
  • Local Storage

How to clear the cache between tests in Cypress

  • Method 1: Using cy.clearCookies () command
  • Method 2: Using cy.clearLocalStorage () command
  • Method 3: Using cy.clearAllSessionStorage () command
  • Method 4:Using Cypress CLInpx cypress cache clear command

This guide explains how to clear Cypress hoard through various method and the best practices associated with it.

What does Cypress Cache include?

The Cypress cache stores files that assistant rush up examination tally and avoid repeated downloads. It typically include:

  1. Cypress Binary Executables:These are the downloaded versions of Cypress, including the Electron browser that get bundled with it.
  2. Installed Cypress Versions:Each version installed is cached so you can swap between versions across labor without re-downloading.
  3. Dependencies and Support Files:Cypress may cache supporting files such as Node modules or browser-related components required to run trial.
  4. Downloaded Browsers:If Cypress is configured to run tests in specific browser (e.g., Chrome, Firefox), these may also be hoard when installed through the Cypress CLI.

Default Cache Location:

  • /Linux:
~/.cache/Cypress
  • :
% LOCALAPPDATA % \Cypress\Cache

Different Types of Cache Storage

Understand each of these mechanics.

Cookies

Cookies are used to save info about the user, such as name, parole, and settings. The storage capacity is limited to a few kilobytes of data. Cookies are a good option for store data that should not be stored for a long clip, such as session IDs. Data is sent with the client & # 8217; s request to the server. Data is store only in the browser.

Session Storage

Session storage is a type of web depot that allow web applications to store datum topically in the user & # 8217; s browser. Unlike cookies, the information and datum stored in session memory are specific to the website for which it was created and is not share with other site. Data is stored solely for the duration of the session and is deleted when the browser is closed.

Session memory is useful for storing sensitive information such as login credentials. Information is not send to the server at the asking of the customer. Data is stored only in the browser.

Local Storage

Local storage is a type of online storage that allow JavaScript to store and access information straight in the browser. This is especially utilitarian if you salve info that you want to proceed yet when the exploiter closes the browser (e.g. setting or preferences).

In-memory data is store as key/value pairs. The key is like the name of the information, and the value is like the datum itself. You can reckon of it as a JavaScript variable. To relieve data to local depot, you must first return a key. You can so store the information you need under that key. Saved data is not blue-pencil when closing the browser. This is especially useful if you save information that you desire to keep even when the exploiter closes the browser (e.g. scene or preferences). Information is not sent to the waiter at the request of the client. The datum is stored in the browser and the system.

For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.

Read More:

Cache depot in Cypress

By nonpayment, Cypress mechanically clears the cache between tests, namely cookies, local depot, and session storage, to prevent the state from build up. You can conserve session details across tests using thecy.session () command.

Though Cypress automatically clears cookies, local storage, and session storage before each test, it also cater specific commands in cause you & # 8217; re habituate it to clear a specific cookie inside a single test or exam isolation is disable.

Also Read:

How to unclutter the cache between tests in Cypress

Use the next examples to see how Cypress brighten each of the test data with the commands.

Method 1: Using cy.clearCookies () bid

The following script opens amazon.com and let cookies. After you run thecy.clearCookies ()command, you can see that the cookie storehouse is empty.

it ('Browserstack cookie ', () = & gt; {cy.visit ('https: //www.amazon.com ') cy.wait (10000) cy.getCookies () .should ('not.be.empty ') cy.clearCookies () cy.getCookies () .should ('be.empty ')})

The undermentioned image establish the answer of thecy.getCookies ()command returning an regalia of 8 objects, while after clearing cookies, an empty raiment is render.

Method 2: Using cy.clearLocalStorage () command

Though Cypress unclutter local storage between tests, you will see how to unclutter local store intentionally. The undermentioned book out-of-doorsamazon.comand asserts to assure that the local entrepot is not empty. After you run thecy.clearLocalStorage ()command, the affirmation to see that the local storage is empty verifies itself.

it ('Browserstack local storage ', () = & gt; {cy.visit ('https: //www.amazon.com ') cy.wait (10000) cy.getAllLocalStorage () .should ('not.be.empty ') cy.clearLocalStorage () cy.getAllLocalStorage () .should ('be.empty ')})

The following image shows the result of thecy.clearLocalStorage ()bid resulting in the asseveration be verified.

Like what you are reading?

You can get discussing with our dissension community

Method 3: cy.clearAllSessionStorage () bidding

The undermentioned playscript opens thehttps: //www.bstackdemo.com/signinpage and signaling in with a valid username and password. After thecy.clearAllSessionStorage ()bid executes and the script tries to open the same page, the page requests for the credentials again.

it ('BstackDemo clear session storage ', () = & gt; {cy.visit ('https: //www.bstackdemo.com/signin/ ') cy.get (`` # username '') .click (); cy.xpath (`` //div [text () ='demouser '] '') .click (); cy.get (`` # password '') .click (); cy.xpath (`` //div [textbook () ='testingisfun99 '] '') .click (); cy.get (`` # login-btn '') .click (); cy.wait (5000); cy.clearAllSessionStorage ({log: true}) cy.visit ('https: //www.bstackdemo.com/signin/ ')})

The undermentioned image establish the signed-in user when credentials are entered:

After the cy.clearAllSessionStorage ()command executes, the following sign-in page seem:

Read More:

Method 4: Cypress CLI Command to Clear Cache

While Cypress provides in-test commands likecy.clearCookies () or cy.clearLocalStorage ()to readjust browser-level hoard during test test, there are situations where you need to clearCypress & # 8217; s internal cache, such as cached binaries, download versions, or corrupted installs. This is handled using theCypress CLI cache command.

Command:

bash

npx cypress hoard open

Talk to an Expert

Good Practices for clearing cache between examination in Cypress

Some of the best practices that you must remark when using these three commands are:

  • Commands need being chained off ofcy
  • Commands can not have any assertions chained
  • Commands should ne'er clip out

Why choose BrowserStack to run Cypress Tests?

Running Cypress test topically or on in-house substructure get with limitations—restricted OS and browser coverage, high maintenance, and circumscribed scalability. BrowserStack Automate provides a robust, cloud-based solution to defeat these challenges.

  1. Real Device and Browser Coverage:Test your coating on real background and mobile browser across multiple variant of,,, and to ascertain all-inclusive compatibility.
  2. No Setup Required:Eliminate the need for local infrastructure apparatus or browser installation. You can fulfil your Cypress tests out-of-the-box utilize BrowserStack ’ s cloud substructure.
  3. Comprehensive Debugging and Reporting:Access detailed test logs, picture transcription, screenshots, and error traces to debug and fix issue speedily. Built-in examination analytics help monitor test health over time.
  4. Seamless Integration:BrowserStack integrates easily with popular CI/CD tools like GitHub Actions, Jenkins, GitLab, and CircleCI, enabling automated Cypress test execution in your deployment pipelines.
  5. Execution at Scale:Run multiple Cypress exam in parallel across different surroundings to importantly reduce test executing time and amend liberation velocity.

Conclusion

This article excuse how Cypress has the inbuilt ability to clear stash data between test run in a single spec file. Though Cypress is robust and offers ease of creating tests, it does limit the reach of browser and device that you can test in real-time position.

For stable examination, you must and for that employ a, such as, is a great option. Access to multiple devices helps you predict and manage matter before your product reaches production. provides access to an array of web browsers and OS for you to test your covering.

Tags
48,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free