Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear site data equivalent in Javascript?

Tags:

javascript

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

like image 489
Calvin Avatar asked Aug 10 '26 16:08

Calvin


1 Answers

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();
like image 200
DevD Avatar answered Aug 12 '26 05:08

DevD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!