Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable and uninstall a Drupal module programmatically?

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.

like image 954
Ben Marshall Avatar asked Dec 04 '12 17:12

Ben Marshall


People also ask

How do I Uninstall a Drupal module?

In the Manage administrative menu, navigate to Extend > Uninstall (admin/modules/uninstall) where you will find the list of modules that are ready to be uninstalled. Check the boxes for the modules you are uninstalling (Search, History, and Activity Tracker). Click Uninstall at the bottom of the page.

How do I disable a Drupal database module?

SELECT name,status FROM system WHERE type='module' AND status='1'; See if a particular module is enabled: SELECT name,status FROM system WHERE name='module_name'; Disable your module, set the status to 0 for the module name that you want to disable.


3 Answers

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

like image 200
Ben Marshall Avatar answered Sep 22 '22 03:09

Ben Marshall


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']);

}
like image 37
leymannx Avatar answered Sep 25 '22 03:09

leymannx


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.yml devel: 0 If you want to uninstall you have to remove devel: 0 from the core.extension.yml

like image 36
Rookie Rod Avatar answered Sep 24 '22 03:09

Rookie Rod