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 it was not possible to find the source. When I had similar problems in Magento 1, the first step I would take was to 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 modules in two ways: using the bin/magento command, or by editing the app/etc/config.php file. My preferred way is using the bin/magento way, as it follows the Magento way.

Disabling modules

Disabling 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 disable all non‑Magento modules? We can use 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]
Michiel Gerritsen
About the author

Michiel Gerritsen

Connect on LinkedIn

Founder of Control Alt Delete, a Magento agency specialised in testing, CI/CD and checkout integrations. Working with Magento since 2015, and board member of Mage-OS.

Missing anything?
What are you missing? X
Thank you for your feedback!