javascriptcachestorage

caches.delete is not deleting anything


Im trying to write a function that will accept a string and remove from cache memory the correct item. My function is:

export const removeItemFromCache = async (name: string | null) => {
  console.log('got here with name:', name)
  if (!name) return;
  await caches.delete(name);
};

In the console, I see got here with name: files/4325 and I clearly have an item with the same name.

The way I use this function is:

  await removeItemFromCache(file.name);

but the file remains in the cache memory even after I refresh the cache or even reload the page. Am I missing something / doing something wrong?

I used this https://developer.mozilla.org/en-US/docs/Web/API/CacheStorage/delete as a reference.


Solution

  • This function worked for me:

      export const removeItemFromCache = async (name: string) => {
      for (const entry of await caches.keys()) {
        window.caches.open(entry).then(async cache => {
          return await cache.delete(name);
        });
      }
    };