Jump to content

There is no officially supported way to click or emulate a click of the "Clear site data" button, but you can achieve something similar with this script stolen from StackOverflow:

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();

A bit of digging on StackOverflow shows that someone managed to find the script that the DevTools button would run, but the link to the official code seems dead. They have, however, posted a snippet in the StackOverflow post.

Link to post
Share on other sites

On 3/20/2025 at 9:22 AM, Ominous said:

There is no officially supported way to click or emulate a click of the "Clear site data" button, but you can achieve something similar with this script stolen from StackOverflow:

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();

A bit of digging on StackOverflow shows that someone managed to find the script that the DevTools button would run, but the link to the official code seems dead. They have, however, posted a snippet in the StackOverflow post.

It works, but it doesn't delete all that I need; some can't be deleted by code. Therefore, a function that specifically presses that button would be necessary.

Link to post
Share on other sites

On 3/24/2025 at 10:40 PM, apoyusiken said:

choose the button with query selector and then call button.click

You can't do this; the DevTools are inaccessible through JavaScript.

 

On 3/24/2025 at 10:31 PM, Arthur de Melo 3575 said:

It works, but it doesn't delete all that I need; some can't be deleted by code. Therefore, a function that specifically presses that button would be necessary.

If JavaScript cannot touch data stored locally, there is a security reason for that, and there is no workaround.

 

If you have access to the server, you can get close to emulating the button by sending a Clear-Site-Data HTTP header to the client. The header can remove HTTP-only cookies and execution contexts. It cannot remove Cross-origin resources and must be triggered by the server for security reasons.

 

Using a wildcard will instruct the client to remove everything it can:

HTTP/1.1 200 OK
Clear-Site-Data: "*"

Using Express as an example:

app.get('/clear', (req, res) => {
  res.set('Clear-Site-Data', '*');
  res.send('Site data cleared!');
});

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×