How to completely clear localstorage
, sessionStorage
and cookies
in javascript ?
Is there any way one can get these values back after clearing them ?
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.
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.
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)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With