Skip to content

Instantly share code, notes, and snippets.

@mikaelvesavuori
Created December 24, 2024 19:55
Show Gist options
  • Save mikaelvesavuori/c5374a49f36ab33edd305a7757cf9237 to your computer and use it in GitHub Desktop.
Save mikaelvesavuori/c5374a49f36ab33edd305a7757cf9237 to your computer and use it in GitHub Desktop.
Clear cookies, storage, and everything
(async function clearAllStorage() {
// Clear all cookies
document.cookie.split(";").forEach(function (cookie) {
const domainParts = window.location.hostname.split(".");
const pathParts = window.location.pathname.split("/");
domainParts.forEach((_, index) => {
const domain = domainParts.slice(index).join(".");
pathParts.forEach((_, pathIndex) => {
const path = pathParts.slice(0, pathIndex + 1).join("/") || "/";
document.cookie = cookie.replace(
/=.*/,
"=; expires=" + new Date(0).toUTCString() + "; path=" + path + "; domain=" + domain
);
});
});
});
console.log("Cookies cleared!");
// Clear localStorage
localStorage.clear();
console.log("localStorage cleared!");
// Clear sessionStorage
sessionStorage.clear();
console.log("sessionStorage cleared!");
// Clear IndexedDB
const dbs = await indexedDB.databases();
dbs.forEach((db) => {
indexedDB.deleteDatabase(db.name);
console.log(`IndexedDB '${db.name}' deleted!`);
});
// Clear Cache Storage
if ('caches' in window) {
const cacheNames = await caches.keys();
await Promise.all(
cacheNames.map((cacheName) => caches.delete(cacheName))
);
console.log("Cache Storage cleared!");
}
// Clear Service Workers (Optional)
if ('serviceWorker' in navigator) {
const registrations = await navigator.serviceWorker.getRegistrations();
await Promise.all(
registrations.map((registration) => registration.unregister())
);
console.log("Service Workers unregistered!");
}
console.log("All storage types have been cleared for this site.");
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment