Knowledge Base Hub

Browse through our helpful how-to guides to get the fastest solutions to your technical issues.

Home  >  How-Tos  >  How to Clear Cache in JavaScript?

How to Clear Cache in JavaScript?

 2 min

Introduction

When you build or roll out a web app, old JavaScript files hung up in a user’s browser can show stale pages. A simple solution to ensure they always get the latest version is to tell their browser to fetch the newest version of this script. This technique is commonly known as cache busting.

JavaScript can’t wipe the whole cache like the user-triggered Clear History button, but clever patterns let devs force new downloads.

Ways to Clear or Bypass Cache in JavaScript

1. Use Query String Versioning (Cache Busting)

The classic way to bypass or clear JavaScript cache files is by putting a version number or timestamp right on the file link.

<script src="script.js?v=2.0"></script>

Or build it on the dynamic level:

const script = document.createElement('script');

script.src = 'script.js?v=' + new Date().getTime();

document.head.appendChild(script);

The browser’s cache sees a unique URL and dutifully fetches the file again.

2. Set Cache-Control Headers (Server-Side)

This trick is not handled by JavaScript and hence resides outside JavaScript. Yet configuring HTTP headers such as Cache-Control and Expires on your server is crucial for preventing long-term caching of your web resources.

Example (Apache .htaccess):

<FilesMatch "\.(js|css)$">

Header set Cache-Control "no-cache, no-store, must-revalidate"

</FilesMatch>

3. Disable Caching Temporarily During Development

When you build an app, browsers let you turn off caching straight in the DevTools menu. If you want something automated, add a random query string or let your service worker pull its own skip-cache rule in JavaScript.

4. Use Service Workers with Custom Caching Logic

In a Progressive Web App, a service worker saves files for offline use. You can wipe those stored files with this short script:

caches.keys(). then(function(names) {

for (let name of names)

caches.delete(name);

});

That loop goes through every named cache and clears the cache for specific JavaScript files.

Best Practices for Cache Management

On a live site, clearing caches too often hurts speed and hurts users. Tag your JavaScript, CSS, and images with version notes so the browser knows when to fetch fresh copies.

Talk with your team about how caching works so mistakes don’t crop up at launch.

FAQs

Why would I need to clear the browser cache using JavaScript instead of manually?

Doing it in code makes sure everyone gets the newest file right away after a push. That small step keeps the site feeling responsive and stops old, broken scripts from showing up.

How does JavaScript clear or bypass the browser cache?

While JavaScript can’t wipe the whole browser cache, it can trick the browser into treating a request as brand-new by adding a timestamp or version string.

Can JavaScript remove all kinds of browser cache (e.g., disk, memory, service worker)

No. JavaScript can access service worker caches with the Cache API but can’t effectively clear disk or memory cache because of browser security policies.

Are there any performance consequences of regularly clearing the cache using JavaScript?

Yes. Turning off cache or forcing new requests repeatedly will decrease load times, bump up bandwidth utilization, and compromise user experience—use it well.

For our Knowledge Base visitors only
Get 10% OFF on Hosting
Special Offer!
30
MINS
59
SECS
Claim the discount before it’s too late. Use the coupon code:
STORYSAVER
Note: Copy the coupon code and apply it on checkout.