How to disable and uninstall a Drupal module programmatically? How to disable and uninstall a Drupal module programmatically? php php

How to disable and uninstall a Drupal module programmatically?


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


Drupal 8

In Drupal 8 this is only one step now.

To be placed inside MYMODULE.install:

/** * Uninstall Field UI. */function MYMODULE_update_8001(&$sandbox) {  \Drupal::service('module_installer')->uninstall(['field_ui']);}


In Drupal 8 you can use configuration API to enable and disable modules.For example, if you want to enable devel module. You have to add following code to the core.extension.ymldevel: 0If you want to uninstall you have to remove devel: 0 from the core.extension.yml