How to clear localstorage, sessionStorage and cookies in javascript? and then restore?

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

Can these values ​​be returned after they are cleared?

+48
javascript
Apr 04 '13 at 6:55
source share
3 answers

how to completely clean 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 return a value after clearing this data?

No , no. But you should not rely on this if it is related to a security issue.

+133
Apr 04 '13 at 7:19
source share
β€” -

It is not possible to get localStorage, sessionStorage, or cookie values ​​using javascript in a browser after they have been deleted using javascript.

If you really ask if there is another way (from outside the browser) to restore this data, another question and answer will completely depend on the particular browser and how it implements the storage of each data of these types.

For example, Firefox stores cookies as separate files. When a cookie is deleted, its cookie is deleted. This means that the cookie can no longer be accessed through the browser. But we know that from outside the browser, using system tools, sometimes the contents of deleted files can be extracted.

If you want to explore this further, you will need to find out how each browser stores each data type on each platform of interest, and then investigates whether this type of storage has any recovery strategy.

0
Apr 04 '13 at 7:03
source share

Standard Web Storage says nothing about restoring any of them. Thus, there will be no standard way to do this. You need to go through the browser to implement them or find a way to back them up before deleting them.

0
Apr 04 '13 at 7:10
source share



All Articles