Remove and uninstall module in a single patch One of the modules I maintain contains a sub-module. The sub-module doesn't do much; just provides views integration for the main module, i.e. only have implementation of hook_views_data. I would like to move this implementation to the main module.

If it was D7, I would have followed the steps below:


Remove the module files.
Write an update hook in the main module, to remove the entry from the system table.
In the same update hook, would delete the variables (if present).


For D8, I have written an update hook for the same in the module (Reference : #2781805 : Social stats field are not available in views):

/**
 * Implements hook_update_N().
 *
 * Removes entry of social_stats_views module.
 */
function social_stats_update_8001(&amp;$sandbox) {
  // Read the configuration.
  $module_data = \Drupal::config('core.extension')-&gt;get('module');

  // Unset the modules you do not need.
  unset($module_data['social_stats_views']);

  // Write the configuration.
  \Drupal::configFactory()-&gt;getEditable('core.extension')-&gt;set('module', $module_data)-&gt;save();
}


I did not add anything else (like removing config, etc.), as the module only implemented a single hook.

I still see the following error after executing the update hook:


  User warning: The following module is missing from the file system: social_stats_views in drupal_get_filename() (line 236 of core/includes/bootstrap.inc).


Is there a bullet-proof way to remove the module?
You can use ModuleInstaller::uninstall():

\Drupal::service('module_installer')-&gt;uninstall(['social_stats_views']);


In D8 there is a lot of housekeeping to do. You can see this in the code of uninstall():

https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Extension!ModuleInstaller.php/function/ModuleInstaller%3A%3Auninstall/8.2.x