Magento 2 6/13/2018

Removing unused core modules from Magento 2 - the right way

Inspired by a tweet by Erik Hansen, I take up the opportunity to write down a technique which is used in our projects frequently. I have first seen it introduced by my colleague Fabian Schmengler.

As described in our article Cut load times by up to 40% - Why and How to disable unused Magento 2 core modules, it sometimes makes sense to disable core modules for performance or security reasons. There, I presented the default way to disable a module:

bin/magento module:disable Magento_Marketplace

However, there are two drawbacks of that method:

  1. The module files are still there, the module is just disabled and can be enabled again by accident.
  2. If you run integration tests, they are run with all modules active - including the disabled modules. Thus, we need a method to remove the modules.

Composer to the Rescue

As those modules are loaded via composer as dependencies, we cannot simply remove them from the composer.json. Instead, we use the replace functionality of Composer to replace them with nothing:

    "require": {
        [...]
    },
    "replace": {
        "magento/module-marketplace": "*"
    },
    "config": {
        [...]

If you call composer update now, the module(s) will be removed from the codebase.

Example for Magento 2.2.3

In one of our projects, this is the composer setup we use:

    "require": {
        [...]
    },
    "replace": {
        "magento/module-admin-notification": "*",
        "magento/module-dhl": "*",
        "magento/module-fedex": "*",
        "magento/module-marketplace": "*",
        "magento/module-multishipping": "*",
        "magento/module-captcha": "*",
        "magento/module-persistent": "*",
        "magento/module-catalog-rule-configurable": "*",
        "magento/module-authorizenet": "*",
        "magento/module-google-adwords": "*",
        "magento/module-sample-data": "*",
        "magento/module-send-friend": "*",
        "magento/module-swagger": "*",
        "magento/module-swatches": "*",
        "magento/module-swatches-layered-navigation": "*",
        "magento/module-tax-import-export": "*",
        "magento/module-google-optimizer": "*",
        "magento/module-ups": "*",
        "magento/module-encryption-key": "*",
        "magento/module-usps": "*",
        "magento/module-braintree": "*",
        "magento/module-webapi-security": "*",
        "magento/module-signifyd": "*",
        "temando/module-shipping-m2": "*",
        "shopialfb/facebook-module": "*"
    },
    "config": {
        [...]

Extended Example for Magento 2.2.4

For Magento 2.2.4 or following, you'll probably want to add some more modules to the list. Our list looks as follows:

    "require": {
        [...]
    },
    "replace": {
        "magento/module-admin-notification": "*",
        "magento/module-dhl": "*",
        "magento/module-fedex": "*",
        "magento/module-marketplace": "*",
        "magento/module-multishipping": "*",
        "magento/module-captcha": "*",
        "magento/module-persistent": "*",
        "magento/module-catalog-rule-configurable": "*",
        "magento/module-authorizenet": "*",
        "magento/module-google-adwords": "*",
        "magento/module-sample-data": "*",
        "magento/module-send-friend": "*",
        "magento/module-swagger": "*",
        "magento/module-swatches": "*",
        "magento/module-swatches-layered-navigation": "*",
        "magento/module-tax-import-export": "*",
        "magento/module-google-optimizer": "*",
        "magento/module-ups": "*",
        "magento/module-encryption-key": "*",
        "magento/module-usps": "*",
        "magento/module-braintree": "*",
        "magento/module-webapi-security": "*",
        "magento/module-signifyd": "*",
        "magento/module-analytics": "*",
        "magento/module-catalog-analytics": "*",
        "magento/module-customer-analytics": "*",
        "magento/module-quote-analytics": "*",
        "magento/module-review-analytics": "*",
        "magento/module-sales-analytics": "*",
        "magento/module-wishlist-analytics": "*",
        "temando/module-shipping-m2": "*",
        "dotmailer/dotmailer-magento2-extension": "*",
        "shopialfb/facebook-module": "*",
        "klarna/module-kp": "*",
        "klarna/module-ordermanagement": "*",
        "klarna/module-core": "*",
        "amzn/amazon-pay-and-login-magento-2-module": "*",
        "vertex/module-tax": "*"
    },
    "config": {
        [...]

Extended Example for Magento 2.2.6

Only one thing has changed in Magento 2.2.6 compared to 2.2.4 and 2.2.5: The amazon pay module has been split into smaller modules. The updated snippet which we use as a base for projects looks as follows:

    "require": {
        [...]
    },
    "replace": {
        "magento/module-admin-notification": "*",
        "magento/module-dhl": "*",
        "magento/module-fedex": "*",
        "magento/module-marketplace": "*",
        "magento/module-multishipping": "*",
        "magento/module-captcha": "*",
        "magento/module-persistent": "*",
        "magento/module-catalog-rule-configurable": "*",
        "magento/module-authorizenet": "*",
        "magento/module-google-adwords": "*",
        "magento/module-sample-data": "*",
        "magento/module-send-friend": "*",
        "magento/module-swagger": "*",
        "magento/module-swagger-webapi": "*",
        "magento/module-swatches": "*",
        "magento/module-swatches-layered-navigation": "*",
        "magento/module-tax-import-export": "*",
        "magento/module-google-optimizer": "*",
        "magento/module-ups": "*",
        "magento/module-usps": "*",
        "magento/module-braintree": "*",
        "magento/module-signifyd": "*",
        "magento/module-analytics": "*",
        "magento/module-catalog-analytics": "*",
        "magento/module-customer-analytics": "*",
        "magento/module-quote-analytics": "*",
        "magento/module-review-analytics": "*",
        "magento/module-sales-analytics": "*",
        "magento/module-wishlist-analytics": "*",
        "temando/module-shipping-m2": "*",
        "dotmailer/dotmailer-magento2-extension": "*",
        "shopialfb/facebook-module": "*",
        "klarna/module-kp": "*",
        "klarna/module-ordermanagement": "*",
        "klarna/module-core": "*",
        "amzn/amazon-pay-sdk-php": "*",
        "amzn/amazon-pay-and-login-with-amazon-core-module": "*",
        "amzn/login-with-amazon-module": "*",
        "amzn/amazon-pay-module": "*",
        "vertex/module-tax": "*"
    },
    "config": {
        [...]

Update for Magento 2.3

There is a seperate blog post about which modules can be removed from Magento 2.3.

Update

There is a valuable addition to that approach, created and shared by Cyrill Schumacher. In his blog post "Magento2 mock classes for removed core modules" he describes how you can remove even more core modules from your project if you don't need them.