Drupal 11: Installing Or Uninstalling Modules Programmatically

The module_installer service can be used to do this.

To install a module:

 \Drupal::service('module_installer')->install(['ban']);

Or, uninstall a module:

 \Drupal::service('module_installer')->uninstall(['ban']);

This is useful in an update hook where you want to ensure that a module is not installed before the configuration is imported.

function mymodule_update_11001($sandbox = NULL) {
  \Drupal::service('module_installer')->uninstall(['ban']);
}

Useful when you have a configuration module installed and it's causing problems with altering the configuration input output streams. You can uninstall the module and remove its effects before importing the configuration.

Add new comment

The content of this field is kept private and will not be shown publicly.