Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all client-side stored data on website using JavaScript

Client-side data can be stored in many different ways (cookies, localStorage, granted permissions, etc.). On my website I want to have a "Clear data" button, and when the user clicks on that one I want to remove all client-side stored data, so there's no trace of the user ever have visited my website on the client's computer. Do web browser contains an API for that? (and if so, how do I use it?) Or do I have to use each individual storage-related API and tell each to delete the data it has stored?

like image 569
Peppe L-G Avatar asked Nov 15 '25 23:11

Peppe L-G


1 Answers

you can call an API and add Clear-Site-Data:"*" in the header of that HTTP response. you can find more information here

if you want to handle it with javascript code, this might help you

sessionStorage.clear()

localStorage.clear()

caches.keys().then(keys => {
  keys.forEach(key => caches.delete(key))
})

indexedDB.databases().then(dbs => {
  dbs.forEach(db => indexedDB.deleteDatabase(db.name))
})

document.cookie = document.cookie.split(';').reduce((newCookie1, keyVal) => {
  var pair = keyVal.trim().split('=')
  if (pair[0]) {
    if (pair[0] !== 'path' && pair[0] !== 'expires') {
      newCookie1 += pair[0] + '=;'
    }
  }
  return newCookie1
}, 'expires=Thu, 01 Jan 1970 00:00:00 UTC; path:/;')
like image 167
Ehsan Nazeri Avatar answered Nov 17 '25 22:11

Ehsan Nazeri



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!