How to use Cypress Clear Cookies Command?

On This Page How do Cypress cookies work?April 01, 2026 · 4 min read · Tool Comparison

How to use Cypress Clear Cookies Command?

Before become started, let us interpret what cookies are. Cookies are pieces of information that are stored by websites in our browsers. This information will be utilized by the various websites based on their need. Sometimes there may be a motive to unclutter those biscuit to screen a website.

How do Cypress cookies work?

Cypress runner/browser get use of cookies to store user-related information. By default, removes the cookies before executing the script. But sometimes, we may involve to brighten our cookies in between while executing our trial script.

Cypress has render the undermentioned bid to clear our biscuit manually:

  • cy.clearCookie ()
  • cy.clearCookies ()
  • cy.clearAllCookies ()

We can pass arguments and clear cookies establish on our needs. Each of the three dictation has its intent. We will look into it with more details below.

Learn More: ?

Benefits of employ the Cypress Clear Cookies Command

For some test scenarios, we may need to log in with multiple exploiter in the same domain for a which might need to clear our biscuit to achieve it.

  • Using clear cookies commands supply by Cypress, we can log in with multiple user account as required by the test case.
  • Also, it improves our browser performance, gives best control over cookie data, and increase web apps & # 8217; protection.

Let ’ s see how Cypress clears cookies between tests from our script.

Types of Cypress Clear Cookies Commands

Cypress mechanisation make it rattling simple and easy to clear cookie with the below three commands:

1. For clearing one particular cookie

cy.clearCookie (name, pick)

2. For clearing multiple cookie with specific domain

cy.clearCookies (options)

3. For clear all cookies across different domain

cy.clearAllCookies (choice)

Using Cypress Clear Cookies Command

Clear Cookie commandArgumentsDescriptionExample
clearCookiename, selection
  1. This is the name of the cookie which has the specific value
  2. Options can get below values in the descriptor of aim

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

  • domain & # 8211; gens of the domain for which you ask to unclutter cookies
  • log & # 8211; A boolean value to make the clear cookie dictation showing in cypress execution window
  • Timeout & # 8211; Waiting time for cypress runner
cy.clearCookie(& # 8220; _gat

_browserstack & # 8221;)

Above bid clears the cooky named _gat_browserstack

clearCookiesoptionsOptions can feature below values in the signifier of object

  • domain & # 8211; name of the arena for which you demand to brighten cookies
  • log & # 8211; A boolean value to make the clear biscuit command presentation in cypress execution window
  • Timeout & # 8211; Waiting clip for cypress runner
cy.clearCookies ({dom

ain: & # 8217; browserstack.co

m’,log: false, timeout:1

5000})

Above command clears all the cookies link to the domain browserstack.com

clearAllCookiesoptionsOptions can have below values in the pattern of aim

  • log & # 8211; A boolean value to make the clear cookie command showing in cypress execution window
  • Timeout & # 8211; Waiting time for cypress runner
cy.clearAllCookies ({lo

g: true, timeout:15000})

Above bidding clears all the cooky across all the area

Let ’ s get our hands dirty by executing the open cookies require

1. Clearing a individual cooky with clearCookie command

We are now going to execute a script which could clear a single cooky

describe ('template spec ', () = & gt; {it ('passes ', () = & gt; {//Visiting the browserstack website cy.visit ('https: //browserstack.com ') //Getting the list of cookies which are stored cy.getCookies () //Clearing the cookie named “ _get_browserstack ” cy.clearCookie (`` _gat_browserstack '') //Viewing the cookies back whether the compulsory cookie got deleted cy.getCookies ()})})

Note: It is mandatory forcy.clearCookiepurpose to pass the argument as the name of the cookie. Without specifying the name of the cookie, the bid will fail. Always use clearCookie to clear any specific biscuit.

2. Clearing Multiple Cookies for a Specific Domain

Similarly, now let & # 8217; s clear all the cookies with a specific domain using clearCookies require

describe ('template spec ', () = & gt; {it ('passes ', () = & gt; {//Visiting browserstack website cy.visit ('https: //browserstack.com ') //Getting the leaning of cookies which on stored in browser cy.getCookies () //Clearing the cooky with specific domain cy.clearCookies ({domain: 'browserstack.com ', log: true, timeout:15000}) //Getting all the cookies list currently available cy.getCookies ()})})

Note:It is not mandatory to legislate options as argument. Suppose we want to clear all the cookies limiting to any specific domain. In that cause, we can simply usecy.clearCookies ()command without specifying any arguments that brighten all biscuit for the current domain.

3. Clearing Multiple Cookies for all the Domains

We are now going to clear all the cookies which got stored in cypress with clearAllCookies command,

describe ('template spec ', () = & gt; {it ('passes ', () = & gt; {//Visiting browserstack website cy.visit ('https: //browserstack.com ') //Getting the list of cookies which on store in browser cy.getCookies () //Clearing all the cookies cy.clearAllCookies () //Getting all the cookie list currently available cy.getCookies ()})})

Viewing more detail from the Cypress smuggler

  • When you execute the above part of code, you will see the execution steps in Cypress smuggler.
  • Try to inspect clearCookie and getCookies command.

You can view the browser console log for the cooky which got store and cookies that are cleared while executing the open cooky bidding.

Conclusion:

With that, you might be able to realize how to clear a cookie/cookies in Cypress and where to useclearCookie, clearCookies,andclearAllCookiesbid. Also, the contention which we need to pass for all the command to use effectively.

Cypress is an emerging examination fabric and can test it across multiple devices. Browserstack provides you with a racy base to prove on multiple devices with Cypress for

Tags
30,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