How to remove unused Javascript in WordPress

Updated on May 8, 2023, by

To address Pagespeed “Remove unused javascript”, delay the javascript from marketing plugins and all other non-essential plugins until user interaction.

Use the Asset Cleanup plugin to remove Javascript code from loading.

What defines what plugin/solution you will use, is if the plugin’s javascript you want to be removed offers support for delaying, or if you want to completely remove it from loading.

To adequately have good Core Web Vitals, delay all non-essential Javascript and/or remove unused Javascript assets from loading. To remove unused Javascript, check the in the frontend of the plugins to detect all pages using it, and then remove it from loading on all pages not using Asset Cleanup or Perfmatters.

How to remove unused javascript in WordPress with Wp Meteor: Delay non-essential Javascript

Wp Meteor is a 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 Javascript till user interaction with a few essential exceptions.

image 4

If you have any issues with the delaying of assets, exclude scripts using regular expressions or keywords that match the plugin that are having errors. For a full list with plugins or themes execution compatibility exclusion, click here.

On the exclusions tab, enable the “Exclude script match…” checkbox and paste the problematic plugin keyword or the following code(quick solution):

(?:wp-content|wp-includes)(.*)
/jquery-?[0-9.]*(.min|.slim|.slim.min)?.js
js-(before|after)

image 5

To find the keyword to exclude, 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 the gtag keyword to delay analytics.js

Place the domain keyword on Wp Meteor, enable the checkbox “exclude scripts matching …”, and save changes.

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 the website. WP Meteor is known to have delay issues with the Elementor Offcanvas addon.

If your website has ads or menus and your userbase experience depends on it or you want to prevent any issues that may arise from using the delay javascript feature, add a javascript from its plugin to the execution compatibility exclusions and avoid errors.

E.g.: if you have a website with +40% users on mobile, you may add the menu javascript theme to the excluded javascript from delaying execution to avoid users having to click two times to get the menu to work(overall load time will increase).

If your website has 90%+ customers from desktop, it may be better to delay the mobile menu’s javascript.

How to remove unused javascript in WordPress with Wp Rocket: Delay non-essential Javascript

Wp Rocket updated its logic for delaying javascript, it now delays all javascript, including inline scripts, and has a default list of exclusions(alongside its internal JS exclusion list) that can be included if you’re having errors.

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

Wp Rocket now lists the active installed scripts retrieved on your website, which then you can exclude from delaying:

image

There is also a textbox for placing the javascript you want to be excluded from. Use it to place essential javascript that cannot be delayed:

Delay Javascript Execution Wp Rocket

For a complete plugins or themes execution compatibility exclusion list, click here.

The following formula can be included on WP Rocket to exclude essential Javascript to avoid errors:

(?:wp-content|wp-includes)(.*)
/jquery-?[0-9.]*(.min|.slim|.slim.min)?.js
js-(before|after)

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.

How to remove unused Javascript in WordPress with Asset Cleanup & Perfmatters

Asset Cleanup is compatible with many cache plugins. After installing and enabling the option to load Asset Cleanup’s dashboard, go to any WordPress page or post 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

Remove the unused CSS enabling the “unload on this page” button. Only remove 100% unused JS files, after clicking and interacting with all elements on the page.

image 2

To better assist you with removing unused CSS or JS, change the Assets List Layout to “grouped by size”, which will list assets by size. Test any combination of removed assets using “Test Mode”.

You can use Perfmatters with a cleaner interface as an alternative for Asset Cleanup:

image 2
Perfmatters Remove Unused JS interface

How to remove unused Javascript in WordPress without a plugin using code

If you want to dequeue a javascript asset without using a plugin, do the following on the functions.php file:

/**
 * We will Dequeue the jQuery UI script as example.
 */
function wp_remove_scripts() {
// check if user is admin, therefore only logged-out users wont load the script
	if (current_user_can( 'update_core' )) {
            return;
        } 
	else {
    // Check for the page you want to target
    if ( is_page( 'homepage' ) ) {
        // Remove Scripts
		wp_dequeue_script( 'jquery-ui-core' );
        wp_deregister_script( 'jquery-ui-core' );
	    }
	}
}
add_action( 'wp_enqueue_scripts', 'wp_remove_scripts', 99 );

Can I remove unused Javascript in WordPress with Autoptimize?

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 your WordPress unused Javascript optimized and your pages faster!

Subscribe to our Newsletter for WordPress tips and recommendations!

*We don't send spams

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments