Enable/Disable all non-core modules using Drush

It happens when I’m developing Drupal sites, writing custom modules and get a “502 Bad Gateway – nginx”. It can be hard to debug as 502 errors are usually caused when PHP freaks out for some reasons. There are many ways to detect where the errors are: logfiles that includes system stuff in /var/log/ and the nginx logs, or undo what you did, or disable all non-core modules to see if it works.

But it’s a real hassle to disable modules one by one, and disabling all modules except for the core/required modules can take some time too, so I found this thanks to alecis:

Print all non-core modules to a txt file

drush pm-list --type=module --status=enabled | sed '/\((.*)\)/!d;/[Cc]ore.*-.*[Rr]equired/d;s/.*(\(.*\)).*/\1/' | sort > noncoremod.txt

Disable them

drush pm-disable $(cat noncoremod.txt)

Enable them

This is when you found the problematic modules and you want to enable them back

drush pm-enable $(cat noncoremod.txt)