Course contentsShow
← Web Development
Lesson 2384 of 2,98243. Service Workers Deep DiveFree lesson

Unregistering Service Workers

How to properly unregister service workers and clean up cached resources.

Unregistering Service Workers

You'll learn how to properly remove service workers and clean up cached resources when they're no longer needed.

Why Unregister?

Sometimes you need to completely remove a service worker from a user's browser—perhaps you're retiring a PWA feature, migrating to a different caching strategy, or debugging issues. Unlike updating a service worker (which replaces it with a new version), unregistering removes it entirely and releases browser control back to the network.

Think of it like moving out of an apartment: you don't just leave your furniture behind—you clean out everything you brought.

How to Unregister

You use the unregister() method on the service worker registration object:

navigator.serviceWorker.getRegistrations()
  .then(registrations => {
    registrations.forEach(registration => {
      registration.unregister()
        .then(success => {
          if (success) {
            console.log('Service worker unregistered');
          }
        });
    });
  });

Or for a specific scope:

navigator.serviceWorker.getRegistration('/app/')
  .then(registration => {
    if (registration) {
      registration.unregister();
    }
  });

Cleaning Up Caches

Unregistering does not automatically clear cached data. You must manually delete caches:

caches.keys()
  .then(cacheNames => {
    return Promise.all(
      cacheNames.map(cacheName => {
        return caches.delete(cacheName);
      })
    );
  })
  .then(() => console.log('All caches cleared'));

This is intentional—cached resources might still be useful even without the service worker actively managing them.

Key Takeaway: Unregistering a service worker removes it from controlling your site, but you must explicitly delete caches to fully clean up resources—they don't disappear automatically.