JavaScript – Delete Cookie


Is it possible to delete a cookie in javascript ?

Yes, Javascript allows us to delete a cookie from the website.

Why we delete a cookie ?

1. We will delete cookies, if we dont want our computer to remember our internet browsing history.

2. If we work on a public computer and if we dont want other users to use our cookie data then we will delete my cookies.

3. If we want to protect our privacy information we will delete the cookies.

4. If the website is now working properly (session already logged-in), we will delete our cookies.

How to delete a cookie in javascript ?

Deleting cookie in javascript can be done in three ways,

1. using “expire” attribute.

2. using “max-age” attribute.

3. manually deleting the cookies from browser.

Javascript interview questions basic !!!

Click to Learn More about – Javascript online learning

1. Using “expire” attribute.


Deleting cookie using “expire” attribute can be done by setting the “expire” attribute a past date value.

Note : Past year is mentioned 2015,2016,2018.


Example :

<script>
                        document.cookie="username=Abdul Kalaam; expires=Sat,23 Oct 2015 8:20:10 UTC";
                        document.cookie="username=Sachin Tendulkar; expires=Sun,12 Jan 2016 8:30:11 UTC";
                        document.cookie="username=Rahul Dravid; expires=Mon,12 Feb 2018 9:31:15 UTC";
                </script>

2. Using “max-age” attribute.

Deleting cookie using “max-age” attribute can be done by setting the “max-age” attribute as zero.

Note : max-age=0;

Example :

 <script>
                        document.cookie="username=Abdul Kalaam; max-age=0";
                        document.cookie="username=Sachin Tendulkar; max-age=0";
                        document.cookie="username=Rahul Dravid; max-age=0";
                </script>

3. Manually deleting the cookies from browser.

This is the manual way directly going to browser -> settings -> Clear browsing data and selecting the cookies and clear.