🔥 🥳 Get your Wordpress Pagespeed optimized today for $99 🔥 🥳 Conditions apply.

How to remove unused Javascript in WordPress (2021)

Last updated on October 18, 2021 | By

A safe and modern way to address Pagespeed “Remove unused javascript” is delaying the javascript from marketing plugins and all other non-essential plugins until user interaction.

You can use both delaying plus dequeuing to further improve the overall website experience, by delaying all non-essential Javascript and dequeuing unused Javascript assets. You can check on the front end of the plugins to detect all pages that are using the plugin, and dequeue the javascript from pages not using it with Asset Cleanup.

How to remove unused javascript in WordPress with a free plugin

Wp Meteor is a free, tested, and up-to-date plugin that lets you load javascript after user interaction. Install it on WordPress and move the bar to “Delay until first interaction”. This will delay all your Javascript till user interaction with a few essential exceptions.

image 4

If you have any issues with the delaying of assets, you can exclude scripts using regular expressions or keywords that match the plugin.

image 5

To find the keyword to exclude, you can either use the asset name or use Chrome Dev Tools; click F12 on Chrome, click the “Network” tab, and hover over “Initiator” to see which file triggered the asset loading:

How to find unused javascript using a Chrome Dev Tools
Analytics.js was initiated by www.googletagmanager.com/gtag, we can use gtag keyword to delay analytics.js

Place the domain keyword on Wp Meteor.

It supports a variated range of user interaction events. Wp Meteor also has a timeout function that executes JavaScript at a certain time delay, 1s or 2s delay.

Note that not every plugin is compatible with such delays. You should never delay a script that is essential for the initial render, it will break your website. WP Meteor is not compatible with Nitropack. WP Meteor is known to have delay issues with the Elementor Offcanvas addon.

How to remove unused Javascript with Wp Rocket 3.9

Wp Rocket updated its logic for delaying javascript, it now delays all javascript, including inline scripts, and has a default list of exclusions(alongside internal js exclusion list).

Delaying any 3rd party plugins will also fix the Pagespeed “Reduce the impact of third-party” warning.

The textbox now is for placing javascript you want to be excluded from:

Delay Javascript Execution Wp Rocket

The following formulas will be included by default on WP Rocket and exclude essential Javascript to avoid errors:


The reason behind this formula is as follows:

  • (?:wp-content|wp-includes)(.) – excludes all internal javascript files
  • /jquery-?[0-9.](.min|.slim|.slim.min)?.js – excludes jQuery
  • js-(before|after) – exclude inline scripts. Only inline scripts that are added using wp_add_inline_script() will be excluded.

If you haven’t bought Wp Rocket yet, consider using my reflink for a +10% discount.

Delay JavaScript Wp Rocket 3.9 or higher compatibility exclusions

To prevent any issues that arise from using the delay javascript feature, Wp Rocket has a list of plugins/themes/services that you can add now to avoid it, click HERE for the list.

For Wp Rocket version below 3.9, see our legacy guide HERE.

How to remove unused Javascript with Asset Cleanup

After installing Asset Cleanup, you’re prompt with welcome messages guiding you through the plugin. You can use Asset Cleanup with a compatible cache plugin but avoid enabling overlapping features that may cause issues.

Go to the wordpress pages or posts tabs and you notice a new button “Manage CSS & JS”, click on it and a new page opens with the enqueued files from that specific page.

image 5
New button on the WordPress interface

To better assist you with removing your unwanted css or js, you can change the Assets List Layout to “grouped by size”, which will list assets by size.

image 4

Remove the unused CSS or JS enabling the “unload on this page” button. You can test any combination of removed assets using “Test Mode“.

Remove unused Javascript in WordPress without plugin using code

If you are a professional developer and want to dequeue a javascript asset without using a plugin you can do the following on your functions.php file:

 * We will Dequeue the jQuery UI script as example.
 * Hooked to the wp_print_scripts action, with a late priority (99),
 * so that it is after the script was enqueued.
function wp_remove_scripts() {
// check if user is admin
	if (current_user_can( 'update_core' )) {
	else {
    // Check for the page you want to target
    if ( is_page( 'homepage' ) ) {
        // Remove Scripts
		wp_dequeue_style( 'jquery-ui-core' );
add_action( 'wp_enqueue_scripts', 'wp_remove_scripts', 99 );

How to find unused javascript(unused code) on your website

You can use the newly released treemap from Pagespeed(Lighthouse) to see unused Javascript. Run a Pagespeed test and click “VIEW TREEMAP” and then “Unused Bytes” on the new page. The striped part is the unused bytes from that javascript file.

How to find unused javascript using Pagespeed
The striped part is the unused bytes from that javascript file

Can i use Asset Cleanup and Wp Rocket?

You can use Asset Cleanup and Wp Rocket together, but avoid enabling overlapping functionalities from both to not cause issues.

How to remove unused CSS in WordPress

Check our article on How to remove or reduce unused CSS in WordPress.

Can I remove unused Javascript in WordPress with Autoptimize?

As of 2021, Autoptimize does not offer the option to remove unused Javascript. You can use Wp Meteor and our guide above to remove unused Javascript. Wp Meteor and Autoptimize are compatible and can be used together.

How to remove unused javascript google tag manager/Facebook?

Use our guide to find the keywords to delay the plugin Javascript. It could be fbevents.js, gtag, gtm.js, or use the domain keyword.

Get a Core Web Vitals Boost

Send us your request!