Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to clear localstorage,sessionStorage and cookies in javascript? and then retrieve?

Tags:

javascript

How to completely clear localstorage, sessionStorage and cookies in javascript ?

Is there any way one can get these values back after clearing them ?

like image 562
Gowsikan Avatar asked Apr 04 '13 06:04

Gowsikan


People also ask

How do I clear my local storage cookies?

Press CTRL + Shift + Delete (alternatively: click More - More Tools - Clear browsing data). Choose a time range or select All time to delete everything. Next to Cookies and Cached folders, check the boxes. Click Clear data.

How do I clear localStorage after closing browser?

You can make use of the beforeunload event in JavaScript.window. onbeforeunload = function() { localStorage. removeItem(key); return ''; }; That will delete the key before the browser window/tab is closed and prompts you to confirm the close window/tab action.

Does clearing localStorage clear cache?

Local Storage data will not get cleared even if you close the browser. Because it's stored on your browser cache in your machine. Local Storage data will only be cleared when you clear the browser cache using Control + Shift + Delete or Command + Shift + Delete (Mac)


2 Answers

how to completely clear localstorage

localStorage.clear(); 

how to completely clear sessionstorage

sessionStorage.clear(); 

[...] Cookies ?

var cookies = document.cookie;  for (var i = 0; i < cookies.split(";").length; ++i) {     var myCookie = cookies[i];     var pos = myCookie.indexOf("=");     var name = pos > -1 ? myCookie.substr(0, pos) : myCookie;     document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT"; } 

is there any way to get the value back after clear these ?

No, there isn't. But you shouldn't rely on this if this is related to a security question.

like image 199
Halim Qarroum Avatar answered Oct 07 '22 18:10

Halim Qarroum


There is no way to retrieve localStorage, sessionStorage or cookie values via javascript in the browser after they've been deleted via javascript.

If what you're really asking is if there is some other way (from outside the browser) to recover that data, that's a different question and the answer will entirely depend upon the specific browser and how it implements the storage of each of those types of data.

For example, Firefox stores cookies as individual files. When a cookie is deleted, its file is deleted. That means that the cookie can no longer be accessed via the browser. But, we know that from outside the browser, using system tools, the contents of deleted files can sometimes be retrieved.

If you wanted to look into this further, you'd have to discover how each browser stores each data type on each platform of interest and then explore if that type of storage has any recovery strategy.

like image 30
jfriend00 Avatar answered Oct 07 '22 18:10

jfriend00