55

I've written a Chrome Extension for my library. It uses chrome.storage.local to cache things.

Does anyone know how to drop the cache for testing purposes? I can't really test things anymore as all the data is now in cache. I'd like to drop it and make sure it gets repopulated correctly, etc. How do I do that?

I tried "Refresh"-ing the extension but that did nothing. Removing and adding the extension doesn't appear to clean cache either.

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
bodacydo
  • 75,521
  • 93
  • 229
  • 319
  • 3
    removing extension will remove chrome.storage.local too. You maybe have code that will populate it on very installation so you see the same data, but when extension is removed, chrome will remove its storage too – Wolf War Aug 04 '15 at 15:21
  • Yep, removing the extension works. Fking chrome should have given the dev some kind of warning before clearing the data, while on developer mode. – Pacerier Aug 06 '17 at 05:57

4 Answers4

98

Use chrome.storage.local.clear() and chrome.storage.sync.clear()

The API is asynchronous so to do subsequent actions to the storage, use a callback:

chrome.storage.local.clear(function() {
    var error = chrome.runtime.lastError;
    if (error) {
        console.error(error);
    }
    // do something more
});
chrome.storage.sync.clear(); // callback is optional
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
39

You can also use chrome.storage.local.remove() method if you want to remove any specific or list of specific object from Storage

chrome.storage.local.remove(["Key1","key2"],function(){
 var error = chrome.runtime.lastError;
    if (error) {
        console.error(error);
    }
})
Satish Kumar sonker
  • 1,250
  • 8
  • 15
  • 3
    Is there a developer tool that can be used to see the storage state and allows me to clear it. – rbansal Jul 25 '22 at 02:10
2

Check this out

chrome.storage.local.remove(keyName,function() {
 // Your code
 // This is an asyn function
});

Check full details https://developer.chrome.com/extensions/storage

barbsan
  • 3,418
  • 11
  • 21
  • 28
Hp Sharma
  • 309
  • 3
  • 7
2

This is a old question but those who using new manifest v3 can use session storage type in order to keep variables only on ram. They will be removed if chrome is restarted or extension is reloaded.

Example:

//to set a session value
var session_var = "foo"
chrome.storage.session.set({session_var: session_var}, function () {
    console.log("session value set");
});

//to get session value 
chrome.storage.session.get({session_var: "this is default value"}, function (result) {
    session_var = result.session_var;
});
ibrahim
  • 3,254
  • 7
  • 42
  • 56