Disabling all non Magento modules in Magento 2

Magento, Quick Tip

Recently i was working on a Magento 2 webshop. It had a bug that when you visited the order grid you would get an exception. It was very generic, from the exception trace i was not possible to find the source. When i had a similar problems in Magento 1 the first step i would do was disable all non-Magento modules. This was simple, just move the xml files of the module you wanted disabled out of the app/etc/modules directory and you're done.

With Magento 2 this is different: Here you can disable module in 2 ways: Using the bin/magento command, or editing the app/etc/config.php file. My preferred way is using the bin/magento way, as it follows the Magento way.

MageTested.com (ad)

Do you want your Magento store to be more reliable? Tired of things that suddenly break the checkout process without anyone noticing? You can hire my services to kickstart End-2-End testing for your Magento store. This way you know for sure that your store is behaving as expected before releasing that new feature or that update.

View MageTested.com for more information.

Disabling modules

The disabling of modules is easy: Just run bin/magento module:disable Vendor_ModuleName. It also accepts multiple module names: bin/magento module:disable Vendor_ModuleName Vendor2_ModuleName2. But how do we do disable all non-magento modules? We can using this command:

php bin/magento module:disable $(php bin/magento module:status |grep -vE 'Magento|List|None|^$')

How does this work?

Basically this runs 3 commands:

php bin/magento module:disable

This is pretty straight forward: Disable the specified modules.

php bin/magento module:status

This is also pretty straightforward: It lists all available module. Enabled or disabled.

grep -vE 'Magento|List|^$'

This is where the real magic happens. The output of module:list is piped to grep. Because we are using the -v flag, grep works opposite. It filters out all matches, instead of keeping them. The -E flag is so we can use a regex. The regex searches for:

  • Magento - Magento_Catalog, Magento_CMS, etc.

  • List - The module:status command contains this line: List of disabled modules

  • None - When all modules are enabled, the list of disabled modules contains the None keyword.

  • ^$ - Remove any whitespace.

Wrapping

Because we wrapped the last two commands in an $(), that command is executed first. The output of those commands is then passed to the module:disable command. Basically this command is executed:

php bin/magento module:disable Magento_CMS Magento_Catalog Magento_Theme [etc]
Want to respond?