Course contentsShow
← Web Development
Lesson 813 of 2,98214. Browser StorageFree lesson

Deleting Cached Entries

Removing individual cached responses with cache.delete() and understanding when to clear stale data.

Deleting Cached Entries

What you'll learn: How to remove individual cached responses when they're outdated or no longer needed.

Why Delete Cached Entries?

Just like cleaning out your refrigerator, sometimes you need to remove old or spoiled items from your cache. Resources become outdated, user preferences change, or you simply need to free up storage space. The Cache API gives you precise control over what stays and what goes.

Using cache.delete()

The cache.delete() method removes a specific cached response. It takes a request (either a URL string or a Request object) and returns a Promise that resolves to true if something was deleted, or false if nothing matched.

caches.open('my-cache').then(cache => {
  cache.delete('/old-page.html').then(success => {
    if (success) {
      console.log('Cached entry removed');
    } else {
      console.log('Nothing to delete');
    }
  });
});

When to Clear Stale Data

Consider deleting cached entries when:

  • Content updates: A news article or product page has changed on the server
  • User logout: Remove personalized content from the cache
  • Version changes: New app version means old cached assets are obsolete
  • Error responses: Don't keep caching failed requests
  • Manual refresh: User explicitly requests fresh content

Matching Behavior

By default, cache.delete() must match the request URL exactly, including query parameters. If you need flexible matching, you can pass options (though we haven't covered those yet—that's for later!).

// These are different cache entries
cache.delete('/api/data?user=1');
cache.delete('/api/data?user=2');

Key Takeaway: Use cache.delete() to remove individual cached responses when data becomes stale, keeping your cache clean and users seeing current content.