How to Clear all Cookies using JavaScript? Last Updated : 14 Oct, 2024 Comments Improve Suggest changes Like Article Like Report Cookies allow clients and servers to exchange information via HTTP, enabling state management despite HTTP being a stateless protocol. When a server sends a response, it can pass data to the user's browser in the form of key-value pairs, which the browser stores as cookies. On subsequent requests to the domain, the browser includes this cookie information in the HTTP request header. This process helps retain login details, preferences, and other user-specific information to improve the user experience.Cookies have an expiration date set with the "expires" attribute, after which they are automatically deleted. By modifying the expiry to a past date, cookies can be deleted. The document.cookie property in the DOM allows access and management of cookies, returning a string of all cookies associated with the current document.Syntax:document.cookie = "key=value";Example: The code below illustrates how cookies can be deleted using JavaScript. The code is run on an online editor to demonstrate that only cookies created by your site can be deleted by the code. HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> Clear cookies using Javascript </title> </head> <body> <script type="text/javascript"> document.cookie = "1P_JAR=akjdsbJKHdjhbk"; document.cookie = "CONSENT=YES+IN.en+20170903-09-0"; function displayCookies() { let displayCookies = document.getElementById("display"); displayCookies.innerHTML = document.cookie; } function deleteCookies() { let allCookies = document.cookie.split(';'); // The "expire" attribute of every cookie is // Set to "Thu, 01 Jan 1970 00:00:00 GMT" for (let i = 0; i < allCookies.length; i++) document.cookie = allCookies[i] + "=;expires=" + new Date(0).toUTCString(); displayCookies.innerHTML = document.cookie; } </script> <button onclick="displayCookies()">Display Cookies</button> <button onclick="deleteCookies()">Delete Cookies</button> <p id="display"></p> </body> </html> Output: Note: If the attribute “HTTP” of a cookie is set to “True”, it is not possible to modify any of its attributes using JavaScript. Comment More infoAdvertise with us Next Article How to Clear all Cookies using JavaScript? S sahanabs Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to Make a Cookie Clicker Using HTML and JavaScript? Creating a "Cookie Clicker" game is a great beginner project for learning how to manipulate elements in HTML with JavaScript. The idea is simple: a user clicks on a cookie image, and each click increases the score. We can add more features like auto-clickers, upgrades, or score multipliers, but we'l 3 min read How to clear the content of a div using JavaScript? JavaScript provides the functionality of clearing the content of div. There are two methods to perform this function, one by using the innerHTML property and other by using the firstChild property and removeChild() method. Below are the approaches used to clear the content of a div using JavaScript: 3 min read How to clear cache memory using JavaScript? Clearing cache memory in JavaScript refers to managing or removing stored data like images, files, and resources that browsers save for faster loading. Although JavaScript cannot directly clear the browser cache, developers often use cache-busting techniques to ensure users receive updated content.B 2 min read How to Set & Retrieve Cookies using JavaScript ? In JavaScript, setting and retrieving the cookies is an important task for assessing small pieces of data on a user's browser. This cookie can be useful for storing user preferences, session information, and many more tasks. Table of Content Using document.cookieUsing js-cookie libraryUsing document 2 min read How to create Cookie Consent Box using HTML CSS and JavaScript ? In this article, we will see how cookies are stored using the cookie consent box using HTML, CSS, & JavaScript.Cookies are small pieces of data that are stored on a user's device while browsing the website. Cookies are used to store information for faster access to data and provide a better user 4 min read Like