Plugins Plugin hub Backup SEO Caching Security Forms WooCommerce Images Best backup plugins
Hosting Hosting directory Shared Managed Cloud India
Themes & builders Compare Rank Math vs Yoast
Tools All tools Hosting finder Plugin stack builder Cost calculator Salt generator Theme detector Who is hosting this? Speed test Headline analyzer robots.txt generator Redirect generator Uptime calculator Blog name generator
Best lists Best backup plugins Deals
Guides All guides Speed up WordPress Choose hosting Back up WordPress
How we test

Speed & performance · Guide

How to Reduce Unused JavaScript in WordPress

Short answer

To reduce unused JavaScript in WordPress, run a PageSpeed Insights report and open the Coverage panel in Chrome DevTools to find the scripts with the most unused code. Remove plugins and features you do not use, limit the rest to the pages that need them, then defer or delay the remaining scripts so they load after the page content.

Key takeaways

  • Lighthouse flags a JavaScript file when more than 20 KiB of its code did not run during page load.
  • Unused during one page load does not mean a script is never needed, so check what it does before you remove it.
  • WordPress 6.3 added defer and async loading strategies to wp_enqueue_script for theme and plugin scripts.
  • Delaying JavaScript until a visitor moves the mouse, scrolls, or taps can cut early script work, but it can break menus and forms.
  • Perfmatters Script Manager lets you turn a script off everywhere and add an exception for the one page that needs it.

What is unused JavaScript in WordPress?

Unused JavaScript is code the browser downloads, parses, and compiles on a page load but never runs during that load.

WordPress sites collect it from themes, plugins, and third-party tools. A contact form plugin may load its script on every post, a slider script may load on pages with no slider, and a chat widget may load before any visitor opens it.

Each file adds download time and work on the main thread, which is the part of the browser that runs JavaScript and handles clicks and taps.

How to pass Core Web Vitals in WordPress explains how that extra work shows up in the scores visitors feel.

The fix is not to delete every flagged file. Some code runs only after a click, a form submit, or a scroll, so it looks unused in a single load and still matters.

How do you find unused JavaScript on your site?

Run PageSpeed Insights on a slow page, then open the Coverage panel in Chrome DevTools to see which lines ran and which did not.

Start with PageSpeed Insights because it lists the files and the saving for each one.

The Lighthouse audit behind it is named “Reduce unused JavaScript” and it lists every file with more than 20 KiB of unused code, as Chrome for Developers explains in its Lighthouse unused JavaScript audit.

Then open the same page in Chrome and use Coverage for the detail. The Chrome DevTools Coverage page says the panel shows total bytes and unused bytes for each CSS and JavaScript file, and you can click a row to see unused lines in the Sources panel. Follow these steps:

  1. Open DevTools, press Ctrl+Shift+P on Windows or Cmd+Shift+P on Mac, type “coverage”, and pick “Show Coverage”.
  2. Pick “Per function” or “Per block” at the top of the panel.
  3. Click the Reload button in the middle of the panel to record the load.
  4. Click menus, open the form, and use the page as a visitor would, then stop the recording.
  5. Sort by unused bytes and click the largest JavaScript rows to see which lines never ran.

Coverage was updated on 13 April 2026, as of November 2026. Its bar shows used code in green and unused code in gray. A file that stays mostly gray after you used the page is a strong candidate for removal or per-page control.

How much unused JavaScript does Lighthouse flag?

Lighthouse flags a JavaScript file when more than 20 KiB of its code did not run during the page load.

That 20 KiB limit is the audit threshold in the Lighthouse unused JavaScript audit. Files below that limit are not listed, even if some of their code never ran. The audit also shows the transfer size and the estimated saving for each listed file, so you can rank the work by size instead of guessing.

Treat the saving as a guide, not a promise. Lighthouse loads the page once without clicking, scrolling, or submitting a form.

Code for a mobile menu, a date picker, or a cart drawer will count as unused in that run even when visitors need it later. Check the file in Coverage after you interact with the page before you decide it is waste.

What should you remove first?

Remove plugins, widgets, and features you no longer use, then remove duplicate tools that do the same job.

Start with the largest files from your Lighthouse report. For each one, ask what feature it powers and whether that feature still appears on the site.

A retired slider, an old popup tool, a social feed you removed from the design, and a second analytics tag are common finds. Deactivate the plugin or remove the widget, clear the cache, and run the report again.

Use this order when you rank the work:

  • Dead features first, because they give a saving with no loss to visitors.
  • Duplicate tools next, such as two chat widgets, two analytics tags, or two form plugins.
  • Site-wide scripts for single-page features next, such as a map script that only belongs on a contact page.
  • Heavy third-party embeds last, because they need a replacement or a delay plan, not a quick delete.

How do you stop plugins loading scripts on pages that do not need them?

Limit each plugin script to the pages that use its feature, with a per-page script manager or with conditional code in your theme.

Perfmatters Script Manager is the no-code route. Its Script Manager documentation says you can disable scripts and stylesheets everywhere, on the current URL, or by post type, and add exceptions for the pages that need them.

It gives Contact Form 7 as the example: the plugin loads on every page and post, but you can disable it everywhere and enable it only on your contact page.

Follow these steps in Perfmatters:

  1. Go to “Perfmatters” then “Tools” in the WordPress admin.
  2. Turn on “Script Manager” under the Plugin section and click “Save Changes”.
  3. Visit a page on the front of the site while logged in.
  4. Hover over “Perfmatters” in the admin bar and click “Script Manager”, or add “?perfmatters” to the page URL.
  5. Find the script, grouped by plugin or theme, and set it to off everywhere, on the current URL, or by post type.
  6. Add an exception for the page that needs it, save, clear the cache, and test that page.

Perfmatters also offers a Testing Mode that limits your Script Manager rules to logged-in admins, so you can preview the change before visitors see it. Use it when you are unsure whether a script is needed.

If you prefer code, WordPress gives you wp_dequeue_script() for this job. You call it on the wp_enqueue_scripts hook with the script handle and a condition such as is_page().

Use a late priority, such as 100, so your code runs after the plugin has added its script. Ask a developer to do this when the plugin has dependencies, because removing a script that other scripts depend on can break the page.

How do you defer JavaScript in WordPress?

Defer scripts that need the page content ready first, so the browser parses the HTML before it runs them.

WordPress 6.3 added a loading strategy to wp_enqueue_script(), as of November 2026. The wp_enqueue_script reference says the $args parameter accepts a strategy of defer or async.

A deferred script runs only after the DOM tree has fully loaded, and deferred scripts run in the same order they appear in the page.

An async script runs as soon as it finishes loading, with no guaranteed order, and it may run before the DOM is ready.

That difference decides which one to use:

  • Use defer for theme scripts and plugin scripts that touch the page or depend on other scripts.
  • Use async only for stand-alone scripts with no dependencies, such as some analytics tags.
  • Do not defer jQuery or a script that inline code calls right away, because the inline code may run before the deferred file arrives.

A developer can set the strategy in code like this:

wp_enqueue_script(
    'my-theme-main',
    get_template_directory_uri() . '/assets/js/main.js',
    array(),
    '1.0.0',
    array(
        'strategy'  => 'defer',
        'in_footer' => true,
    )
);

If you do not write code, use a cache plugin instead. LiteSpeed Cache and WP Rocket both add defer options in their settings, and the next two sections show the exact screens.

Do not turn on defer in two plugins at once, because the second pass can change the order and break scripts that were already deferred. How to eliminate render-blocking resources in WordPress explains how defer fits with the other render-blocking fixes.

MethodWhat it doesBest for
RemoveStops the script loading at allDead plugins and retired features
Limit per pageLoads the script only where it is usedContact forms, maps, and sliders
DeferRuns the script after the HTML loadsTheme and plugin scripts that need the page
DelayWaits for a mouse move, scroll, or tapChat, analytics, and ad scripts

How do you delay JavaScript until a visitor interacts?

Delay scripts that no visitor needs in the first seconds, so they load only after a mouse move, scroll, tap, or key press.

WP Rocket calls this Delay JavaScript Execution, and its Delay JavaScript Execution documentation says it delays all JavaScript files and inline scripts until user interaction, such as moving the mouse, touching the screen, scrolling, or pressing a key.

The same page says the option can help the PageSpeed checks for unused JavaScript, main thread work, JavaScript execution time, total blocking time, and first contentful paint. It applies only on cached or optimized pages.

Follow these steps in WP Rocket:

  1. Go to “Settings” then “WP Rocket” then “File Optimization” in the WordPress admin.
  2. Find the JavaScript files section and turn on “Delay JavaScript Execution”.
  3. Click “Save Changes” and clear the cache if the plugin asks.
  4. Open the site in a private browser window, move the mouse, and check that menus, forms, and sliders still work.
  5. If a feature breaks, add its file to “Excluded JavaScript Files” or use the one-click exclusions for that plugin or theme.

WP Rocket also notes that turning this option on disables the Combine JavaScript files feature, so scripts keep the right order. Its Safe Mode for Delay JavaScript Execution excludes scripts under /wp-content/ and /wp-includes/, the jQuery libraries, and inline scripts added with wp_add_inline_script().

Use Safe Mode only as a short-term step while you find the exact files to exclude.

LiteSpeed Cache offers the same choice with two timings.

Its Page Optimization documentation says both Deferred and Delayed hold JavaScript until the HTML has finished loading, but Deferred runs the script as soon as the HTML finishes, while Delayed runs it only after user activity such as a key press or mouse movement.

The page warns you to test Delayed mode on your site before you enable it, because the score gain can cost you a slow menu or popup.

In LiteSpeed Cache, go to “LiteSpeed Cache” then “Page Optimization” then “JS Settings”, set “Load JS Deferred” to Deferred first, save, and purge all cache. Try Delayed only after Deferred works on every page type.

Add scripts that must run early, such as a cookie banner or checkout script, to “JS Deferred/Delayed Excludes” under the Tuning tab.

When should you use code splitting or a lighter script?

Use code splitting when one bundle serves many pages, and swap a heavy library when a smaller tool does the same job.

Code splitting means dividing a JavaScript bundle into pieces and sending only the piece the first page needs. The web.dev code splitting guide says this cuts startup time by cutting the script that must be parsed and compiled before the page can respond.

It also says smaller startup payloads can help Interaction to Next Paint, because the main thread is free to answer input sooner, and can help Largest Contentful Paint when a large script delays the main content.

The same guide shows the pattern with a form: the code for the form action loads with a dynamic import only when the visitor submits the form, instead of loading with the first page.

In WordPress terms, that is the same idea as loading a slider script only on pages with a slider, or a map script only on the contact page.

A developer can apply it to a custom theme or a custom block, while a site owner can get most of the gain from the per-page controls in the section above.

Replace one script at a time.

How do you handle third-party scripts?

List every third-party script, keep only the ones that earn their place, and delay or self-host the rest.

Third-party scripts come from analytics, ads, chat, social embeds, review widgets, and tag managers. They are easy to add and easy to forget, and each one can load more scripts of its own.

Open the Network tab in DevTools, filter by JS, and write down every domain that is not your own. For each one, note what it does, which pages need it, and who asked for it.

Work through the list with these rules:

  • Remove tags for tools you stopped using, including old pixels and retired chat widgets.
  • Keep one tool per job, so you do not run two analytics tags or two chat tools at once.
  • Delay chat, heatmap, and ad scripts until after the first paint or until interaction, using the delay options above.
  • Load social embeds only when the visitor scrolls to them, or replace a live feed with a static image and a link.
  • Ask the vendor whether a lighter script exists before you accept a heavy one as fixed.

How do you check that the fix worked?

Run the same PageSpeed Insights report on the same URLs, compare the unused JavaScript list, and click through every page type.

Give the cache time to rebuild first. Purge the cache after each change, then wait for a fresh page load before you run the report. Compare the same pages: the homepage, a post, a product or service page, and the contact page.

The unused list should shrink, and the largest files from the first report should be gone or much smaller.

Check the site by hand as well as by score. Open each page type in a private window, test the menu on mobile, submit the contact form, open the cart or checkout, and play any slider or video. A script that loads late can pass a lab report and still break a real visit.

If the score did not move, check the obvious causes first. The cache may still serve the old page, the change may apply only to logged-out visitors, or the script may load from a different plugin than the one you changed.

Change one setting at a time, purge, and re-run the report, so you know which change helped. How to improve Interaction to Next Paint in WordPress shows how to read the responsiveness side of the same report.

What mistakes should you avoid?

Do not delete code only because a report flagged it, and do not turn on every delay option at once.

The most common mistake is treating “unused in one load” as “never used”. A script for a form, a menu, or a checkout step may sit idle until a visitor needs it. Remove it and the feature breaks, even though the score improves.

Always interact with the page in Coverage before you remove a file, and test the feature after you remove it.

Other mistakes to avoid:

  • Turning on defer, delay, and combine in two plugins at the same time, which can change script order and break the site.
  • Delaying a cookie banner, a checkout script, or a form script that must run before the first click.
  • Deferring jQuery while inline code still calls it on page load.
  • Skipping the mobile test, because a menu or popup that works on desktop can fail on touch.
  • Changing five settings at once, so you cannot tell which one broke the page or fixed it.

When a page breaks, undo the last change first, purge the cache, and test again in a private window. How to speed up WordPress gives the wider order for speed work, so you can see where script cuts fit next to caching and image work.

Add exclusions one file at a time until the feature works, and keep the exclusion list in your notes.

Frequently asked questions

Can I get unused JavaScript to zero in WordPress?

No, and you should not aim for zero. Some code only runs after a click, a form submit, or a scroll, so it looks unused in a single load and still matters. Aim to remove dead features, limit scripts to the pages that need them, and defer or delay the rest. A small list of flagged files is normal on a working site.

Is it safe to delete a script that Lighthouse flagged?

It is safe only after you know what the script does. Open the file in the Coverage panel, interact with the page, and check whether the feature still works without it. If the script powers a menu, form, slider, or checkout step, keep it or move it to the right pages instead of deleting it. Test every page type after a removal.

Should I defer or delay JavaScript?

Defer first, because deferred scripts still run after the HTML loads and keep their order. Delay is stronger: scripts wait for a mouse move, scroll, tap, or key press, which cuts early work more but can make menus and forms feel slow. Start with defer, test the site, and try delay only on scripts that no visitor needs in the first seconds.

Why does unused JavaScript come back after I fix it?

It comes back when a new plugin, widget, or embed adds its own scripts, often on every page. A new chat tool, review widget, or social feed can undo a clean report in one install. Check the PageSpeed report after each new plugin, limit its scripts to the pages that need them, and remove tools you stop using.

Does reducing unused JavaScript help search rankings?

It helps the experience behind the rankings, not the ranking directly. Less script work can mean faster loading and quicker responses to taps, which are the things Core Web Vitals measure. Google uses real-user Core Web Vitals data in its systems, so a faster, more responsive page is a better page to rank, but script cuts alone do not guarantee a higher position.

Sources

  1. Remove unused JavaScript Chrome for Developers
  2. Coverage: Find unused JavaScript and CSS Chrome for Developers
  3. Reduce JavaScript payloads with code splitting web.dev
  4. wp_enqueue_script() WordPress Developer Resources
  5. Delay JavaScript execution WP Rocket
  6. Page Optimization | LSCache for WordPress LiteSpeed Documentation
  7. How to Disable Scripts on a Per Post/Page Basis (Script Manager) Perfmatters

Is your product missing, or something out of date?

Tell us what to add or correct. We read every note and reply when we need a source. A message does not buy a ranking.

About the editors

WPLegible editors

Researched from the primary sources listed above and checked against our editorial rules. About the site · How we test