Magento 2 is gaining popularity as an open source Ecommerce platform. It has a huge community of developers. It is growing with each new major version by increasing its complexity. Adding lots of new but useful features and modules to its core regularly. It is becoming more modular and it’s module based architecture encourages developers to add new modules frequently.
We developers sometimes add 3rd party modules to our Magento 2 installation when ever required. Removing these 3rd party extensions and our own developed extensions is easy. But what about the core modules? Have you every come across situations where you wish you could entirely remove a core Magento 2 module that seems like of no use to you?
Yes, just disabling a core module is not enough, you have to completely remove the unused module from your Magento 2 installation to make it run faster.
In this tutorial, I will walk you through the process of completely removing Magento 2 core module from your installation. You might have experienced that just disabling certain core module does not work. They are still being executed in some of the processes like setup upgrade etc.
How do you think you will remove Magento 2 core modules?
Just by deleting the source code files from the filesystem in your installation. This will not do the magic though. Any sub sequent composer update command will bring that module back in place in the filesystem. And you will get shocked, how this is possible? Yes, you just removed the source code files from the filesystem but composer has it all listed in its lock file.
Why just removing the module from the file system does not work?
When we install Magento 2 via composer metapackage we install some root level project dependencies those can be found in the composer json require section. These root level packages have their own internal dependencies which are mentioned in the individual composer json files relative to the modules. Due to this ability of maintaining internal dependencies on module levels we do not get any clue how to remove those.
We remove it from the filesystem thinking that will do the job. But what we learned above is modules maintain their dependencies individually on their own composer json file. Therefore, even after we remove the files the dependencies list is still intact in the composer json file. And when we run composer update in near future those modules get restored.
Proper way to remove OR Steps to follow.
- Identify the core module that you think is of no use to you.
- Try the module disable command for the chosen module.
- Disable any additional modules those are linked or depend on the chosen module.
- Mark the modules for complete removal in the project main composer JSON file.
- Execute composer update command from project root directory.
- Run Required Magento 2 CLI commands.
List of a few Magento 2 core modules those need to be completely removed from my installation.
- MSP_TwoFactorAuth (V 2.3.6)
- MSP_ReCaptcha (V 2.3.6)
Let’s check if the target module can be disabled individually.
bin/magento module:disable MSP_TwoFactorAuth
If this command completes its process by disabling the said module, then you are good to go for complete removal of this module form your installation. Else If it says something like “there exists certain other modules those depend on this module and hence can not be disabled individually“, then you have to check with those other modules. You have to decide whether you could disable them as well without any negative effect. Finally you have to put all of those disabled modules in the replace list.
Declaring the module to be removed in composer JSON
It is assumed that we have installed our Magento 2 application using composer. Non-composer based installation will not follow this approach. Let’s update our composer JSON file to include something like below in it’s content. We are basically declaring a replace key in our composer file at the top level.
In case your composer file already have a replace section, then you need to check for the target module in that existing list. If not found in the list then you can add a new line to the list.
Read more about COMPOSER REPLACE here.
"replace": {
"msp/twofactorauth": "*"
}
Here msp/twofactorauth
is the composer key name for the module MSP_TwoFactorAuth
. You can find this composer key name of any module in the composer.json
file for that module. And in the replace section we are instructing composer to replace this module with null version or remove it completely.
Running composer update
Finally we have to run composer update
command from the project root directory. If all goes okay, there is no error in composer JSON file then the command will take some time to run. And finally the module will be removed. Then we have to run necessary Magento 2 console commands.
Having issues during composer update check this article on Magento 2 upgrading composer issues.