How to disable and uninstall a Drupal module programmatically?

Ben Marshall picture Ben Marshall · Dec 4, 2012 · Viewed 13.7k times · Source

I want to be able to disable and then uninstall a module programmatically on my local environment and then easily push that code to prod so everything gets updated.

I'm guessing it's something with the hook_update_N. But not sure where to start.

Answer

Ben Marshall picture Ben Marshall · Dec 4, 2012

Think I found the answer! Within the modules .install file, I added this code:

/**
 * Disable and uninstall the module.
 */
function MODULE_update_7200() {
  if( module_exists('MODULE')) {
    module_disable(array('MODULE'));
    drupal_uninstall_modules(array('MODULE'));
  }
}

The number in the function should reflect your drupal install. See how to number them here: http://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_update_N/7