PageSpeed Insights often reports ‘Reduce unused JavaScript’ and ‘Reduce unused CSS’, listing files where much of the code is not used on the page. Unused code still has to be downloaded, parsed and sometimes run, which slows loading and responsiveness. Here is how to find where it comes from and reduce it without breaking features.
Why unused code exists
Themes, page builders and plugins are built for many sites and layouts, so they ship code for features you may not use on every page. A slider library loads on pages without sliders; a stylesheet contains rules for dozens of widgets; analytics tools load features you never configured.
Some unused code is normal. Code for dropdown menus, popups or interactions is unused until the visitor triggers them. The goal is to remove what is genuinely unnecessary, not every byte that PageSpeed flags.
Find the sources
PageSpeed Insights lists files with the most unused bytes, grouped by origin. Look at which belong to your theme, which to plugins and which to third parties such as Google Tag Manager, Facebook, chat widgets or YouTube.
Chrome DevTools’ Coverage tool shows how much of each CSS and JavaScript file is used as you load and interact with the page. Use it to see what is used after interactions, not just on load.
- 1Find sourcesPageSpeed and Coverage
- 2Third partiesRemove and delay
- 3Per-page loadingAsset manager
- 4Builder settingsPerformance features
- 5Replace heavy featuresSliders, icon fonts
Third-party JavaScript
Third-party scripts are often the largest source of unused JavaScript and the hardest to change, because you do not control their code. Remove tags you no longer need, consolidate analytics into one platform where possible and avoid loading several tag managers.
Delay remaining third-party scripts until after interaction, so they do not compete with the initial load. My guide to third-party scripts explains how to measure and manage them.
Load plugin code only where needed
Use an asset management plugin such as Perfmatters or Asset CleanUp to disable plugin scripts and styles on pages that do not need them. For example, load the contact form plugin only on the contact page and pages with forms.
WooCommerce loads scripts and styles on all pages by default in many setups. You can disable them on non-shop pages, with care to keep cart fragments and mini-cart behaviour working where needed. See speeding up a slow WooCommerce store.
Page builder settings
Elementor, Divi and other builders have performance settings that reduce unused code: loading widget scripts only when used, improved CSS loading, disabling unused features and icon libraries. Turn them on and test.
Remove unused add-on packs or disable unused widgets within them. Each pack can add significant JavaScript and CSS across the site. See how to speed up an Elementor website.
Remove unused CSS
Performance plugins can analyse each page and remove CSS rules not used on it, loading the full stylesheet later or not at all. This reduces CSS size dramatically on heavy themes.
Test interactive elements carefully, as styles for menus, popups and dynamic content may be removed. Add safelist rules for classes that must be kept. Regenerate after design changes.
Delay JavaScript
Delaying non-essential JavaScript until user interaction reduces the work done during load. Lighthouse may still report the code as unused, but it no longer affects initial loading. Real users benefit from a faster first render and better responsiveness.
Exclude scripts that must run immediately, such as consent banners, menus and above-the-fold sliders. See minify, combine and defer safely.
Replace heavy features
Some features are inherently heavy: sliders, animation libraries, complex mega menus, social feeds and large icon fonts. Replacing them with lighter alternatives or simpler designs removes code entirely rather than trying to optimise it.
For example, a static hero image instead of a slider, CSS transitions instead of an animation library and inline SVG icons instead of an icon font. These changes often improve both speed and usability.
Theme and custom code
If your theme loads large libraries such as jQuery plugins or frameworks for small features, a developer can replace them with lighter code or conditional loading. Custom themes can be built to load only what each template needs.
When choosing a new theme, test its baseline unused code with a blank page. Lightweight themes report far less unused code. See choosing a WordPress theme.
Be realistic
You will rarely eliminate these warnings completely, especially with analytics and marketing tools. Focus on the largest files and on code that affects LCP and INP. A reduction from several hundred kilobytes to under a hundred is a meaningful improvement.
Measure real results: LCP, INP and conversions. If a change reduces unused code but breaks a useful feature, it is not worth it.
Getting help
Reducing unused code safely is a core part of my speed optimization service, especially on page builder and WooCommerce sites.
For the overall approach, see how to speed up a WordPress website and how to read PageSpeed Insights.
Key takeaways
- Find which theme, plugin or third party each unused file comes from.
- Load plugin assets only where they are used.
- Delay non-essential JavaScript until interaction.
- Replace heavy features like sliders rather than trying to optimise them.
Frequently asked questions
Why does Google Tag Manager show as unused JavaScript?
Tag managers and analytics load code for many features. Delay or reduce tags where possible.
Can I remove unused CSS automatically?
Yes, with performance plugins, but test interactive elements and safelist needed classes.
Is some unused JavaScript normal?
Yes. Code for interactions is unused until triggered. Focus on genuinely unnecessary code.
Does unused JavaScript affect INP?
Large scripts take time to parse and run, which can delay interactions.
Should I switch themes to reduce unused code?
If optimisation does not help enough, a lightweight theme often makes a big difference.
Does jQuery count as unused JavaScript?
Often partly. Many themes and plugins still depend on jQuery, so removing it can break features. Check which plugins need it before trying, and prefer themes that do not rely on it.
