How to Remove Unused CSS and JavaScript in WordPress for Faster Performance

In my years of optimizing WordPress websites, one of the most overlooked and effective way to improve site speed is removing unused CSS and JavaScript. Unused CSS and JavaScript files clog your site’s performance, increase page load times, and negatively affect user experience. This article provides a step-by-step guide to identifying and removing unused CSS and JavaScript in WordPress, based on my hands-on experience with client sites. I’ll cover tools, manual techniques, and plugins, ensuring your site runs lean and fast.

Why Removing Unused CSS and JavaScript Matters

Unused CSS and JavaScript refers to code that loads on your WordPress site but isn’t contribute to rendering or functionality. For example, a plugin might enqueue styles for every page, even when unused on most. This bloat impacts:

  • Page Load Time: Extra files increase HTTP requests and file sizes.
  • SEO: Google’s Core Web Vitals penalize slow-loading sites.
  • User Experience: Faster sites reduce bounce rates.

A 2023 study by Google showed that a 1-second delay in load time can reduce conversions by 7%. Removing unused CSS and JavaScript is a practical way to optimize performance.

Step 1: Identify Unused CSS and JavaScript

Before removing unused CSS and JavaScript, you need to know what’s unnecessary. Here are tools I rely on:

ToolPurposeFreeFree/PaidLink
Chrome DevToolsAnalyzes CSS/JavaScript coverageFreeGoogle Chrome
Asset CleanUpDisables unused scripts/styles per pageFree/ProAsset CleanUp
PurifyCSSDetects unused CSSFree (CLI)PurifyCSS GitHub
GTmetrixReports unused code impactFree/ProGTmetrix

Using Chrome DevTools

  1. Open your site in Chrome.
  2. Press Ctrl + Shift + I to open DevTools.
  3. Go to the Coverage tab.
  4. Click the record button, reload the page, and view unused CSS/JavaScript (highlighted in red).

This method helped me identify a client’s theme loading 200KB of unused CSS on their homepage.

Step 2: Remove Unused CSS and JavaScript with Plugins

Plugins simplify the process for non-developers. Here are two I’ve used successfully:

Asset CleanUp

This plugin lets you disable CSS/JavaScript files on specific pages. For a client’s e-commerce site, I used it to stop WooCommerce scripts from loading on blog pages, reducing load time by 30%.

Steps:

  1. Install Asset CleanUp.
  2. Go to CSS/JavaScript Manager in your WordPress dashboard.
  3. Select a page and uncheck unused files.
  4. Test thoroughly to ensure functionality isn’t broken.

Perfmatters

Perfmatters offers advanced script management. I’ve used it to defer or remove unused JavaScript, like jQuery, on lightweight landing pages.

Steps:

  1. Install Perfmatters.
  2. Navigate to Assets > Script Manager.
  3. Disable scripts by page or post type.
PluginPluginKey FeaturePrice
Asset CleanUpPer-page script controlFree/$49 Pro
PerfmattersAdvanced defer/remove options$24.95/year

Step 3: Manually Remove Unused CSS and JavaScript

For developers, manual removal offers precision. Here’s how I approach it:

Removing Unused CSS

  1. Identify: Use PurifyCSS or Chrome DevTools to find unused rules.
  2. Edit Theme Files: In your child theme’s style.css, remove unused styles.
  3. Use Critical CSS: Extract critical CSS for above-the-fold content and inline it. Tools like Critical automate this.

Example:

/* Before */
.header, .footer, .unused-class {
  display: none;
}

/* After */
.header, .footer {
  display: none;
}

Removing Unused JavaScript

  1. Deregister Scripts: Use wp_deregister_script() in your child theme’s functions.php.
  2. Example: To remove a plugin’s script:function remove_unused_js() { if (!is_page('contact')) { wp_deregister_script('contact-form-js'); } } add_action('wp_enqueue_scripts', 'remove_unused_js', 100);

I once reduced a site’s JavaScript payload by 150KB by deregistering unused plugin scripts.

Step 4: Optimize Remaining CSS and JavaScript

After removing unused code, optimize what’s left:

  • Minify Files: Use plugins like Autoptimize to compress CSS/JavaScript.
  • Defer JavaScript: Add defer to script tags or use Perfmatters.
  • Combine Files: Reduce HTTP requests by combining CSS/JavaScript files (test carefully).

Fixing Slow WordPress Dashboards with a Lean Admin Tweaks Guide

Step 5: Test Your Site

Removing unused CSS and JavaScript can break functionality if done carelessly. I always:

  1. Test on a staging site first.
  2. Check all pages, especially dynamic ones (e.g., WooCommerce checkout).
  3. Use GTmetrix to confirm performance improvements.

For a client’s portfolio site, this process cut load time from 3.2 seconds to 1.1 seconds.

Common Mistakes to Avoid

  • Removing Critical Scripts: Disabling jQuery might break plugins.
  • Over-Optimization: Combining all files can cause conflicts.
  • No Backups: Always back up before making changes.

Conclusion

Removing unused CSS and JavaScript in WordPress is a proven way to boost site speed and SEO. Whether using plugins like Asset CleanUp or manual methods, the key is careful identification, removal, and testing. In my experience, this process can shave seconds off load times, delighting users and search engines alike.

Leave a Reply

Your email address will not be published. Required fields are marked *