🔥 Just Launched! Werra Premium Template for HyväSee it in Action

How to Remove JS and CSS in Magento 2 Using Layout XML

By Sanjay JethvaUpdated on May 21, 2025 2 min read

Is your Magento 2 store having low efficiency and high loading time? Do you want to have speed optimization of Magento 2 store? While managing a successful E-commerce store, the store owners have to deal with a lot of problems. One such problem is higher page load time.

While creating a store, you may add custom CSS and JS in Magento 2 for attractive layouts and better features. On the other hand, you end up calling numbers of requests and increasing page loading speed. After the page speed analysis, you require to remove JS and CSS in Magento 2 which no longer useful and can improve the page load time. You can also enable merge CSS, javascript in Magento 2 for better performance.

Also, sometimes the confliction between the theme and module may require you to remove JS and CSS from a page.

Today, I’ve come up with the solution to remove JS and CSS in Magento 2 using layout XML which can lead to improved load time of the store.

Programmatic Solution to Remove JS and CSS in Magento 2 Using Layout XML

Create cms_index_index.xml at app/design/frontend/[Namespace]/[theme]/Magento_Theme/layout directory and make use of <remove> tag under the <head> tag to remove JavaScript and CSS.

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <remove src="js/custom.js"/>
        <remove src="css/custom.css"/>
    </head>
</page>

That’s what you need to do to remove JS and CSS in Magento 2 using layout XML.

Do not forget to share the post with Magento Community via social media.

Thank you.

Sanjay Jethva Full Image
Article bySanjay Jethva

Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe. His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.