6

enter image description here

I want to have the same behavior as 'Clear site data' in a Javascript function, because my Angular app (after upgrading Angular) seems to misbehave without clearing site data and I don't want customers to be forced to clear site data themselves.

If it's not possible to clean everything, is there at least a way to clean 1) localStorage 2) all IndexedDB databases 3) Cookies and 4) Web SQL

Thanks in advance

Calvin
  • 347
  • 1
  • 4
  • 18
  • 3
    Does this answer your question? [What is the JavaScript equivalent of "Clear Site Data" in Chrome Dev Tools?](https://stackoverflow.com/questions/42772028/what-is-the-javascript-equivalent-of-clear-site-data-in-chrome-dev-tools) – viam0Zah Jun 30 '20 at 11:11
  • @Calvin did you find a solution to Clear site data? – DevD Dec 02 '22 at 07:04
  • @DevD Unfortunately there is no simple solution. You can change your filenames a bit so browser cache isn't used, loop through localStorage to delete all items etc. But just a simple built in function to delete everything isn't there. – Calvin Dec 03 '22 at 11:46

1 Answers1

3

I created a script to achieve this. Posting here in case anyone needs something similar.

var theCookies = document.cookie.split(';');
for (var i = 1 ; i <= theCookies.length; i++) {
    var acookie = theCookies[i-1];
    var cookieArr = acookie.split('=');
    console.log(cookieArr[0]);
    document.cookie = cookieArr[0]+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
}

// Get cache storage and clear cache storage
window.caches.keys().then(function(names) {
    for (let name of names)
        window.caches.delete(name);
});

// Get indexed db and delete indexed db
const dbs = await window.indexedDB.databases()
dbs.forEach(db => { window.indexedDB.deleteDatabase(db.name) })

// clear localStorage
window.localStorage.clear();

// clear sessionStorage
window.sessionStorage.clear();
DevD
  • 304
  • 1
  • 4
  • 11