diff --git a/core/lib/Drupal/Core/Extension/ModuleInstaller.php b/core/lib/Drupal/Core/Extension/ModuleInstaller.php
index 1f11365..92a1712 100644
--- a/core/lib/Drupal/Core/Extension/ModuleInstaller.php
+++ b/core/lib/Drupal/Core/Extension/ModuleInstaller.php
@@ -14,6 +14,7 @@
 use Drupal\Core\Config\StorageInterface;
 use Drupal\Core\DrupalKernelInterface;
 use Drupal\Component\Utility\SafeMarkup;
+use Drupal\Core\Config\StorageException;
 
 /**
  * Default implementation of the module installer.
@@ -52,6 +53,37 @@ class ModuleInstaller implements ModuleInstallerInterface {
   protected $uninstallValidators;
 
   /**
+   * The extension configuration manager.
+   */
+  protected $extensionConfig = NULL;
+
+  /**
+   * Module Configuration Installer
+   *
+   * @var \Drupal\Core\Config\ConfigInstallerInterface
+   */
+  protected $configInstaller = NULL;
+
+  /**
+   * Entity Manager
+   */
+  protected $entityManager = NULL;
+
+  /**
+   * Configuration Manager
+   *
+   * @var \Drupal\Core\Config\ConfigManagerInterface
+   */
+  protected $configManager = NULL;
+
+  /**
+   * Flag to determine if a rollback will be required.
+   *
+   * @var bool
+   */
+  protected $rollBackRequired = TRUE;
+
+  /**
    * Constructs a new ModuleInstaller instance.
    *
    * @param string $root
@@ -71,6 +103,11 @@ public function __construct($root, ModuleHandlerInterface $module_handler, Drupa
   }
 
   /**
+   * Name of the module we are currently (un)installing.
+   */
+  protected $module = '';
+
+  /**
    * {@inheritdoc}
    */
   public function addUninstallValidator(ModuleUninstallValidatorInterface $uninstall_validator) {
@@ -78,28 +115,43 @@ public function addUninstallValidator(ModuleUninstallValidatorInterface $uninsta
   }
 
   /**
-   * {@inheritdoc}
+   * Get all module data so we can find dependencies and sort.
+   *
+   * @param array $module_list
+   *   By Reference.
+   * @return array
+   *   Module data.
    */
-  public function install(array $module_list, $enable_dependencies = TRUE) {
-    $extension_config = \Drupal::configFactory()->getEditable('core.extension');
-    if ($enable_dependencies) {
-      // Get all module data so we can find dependencies and sort.
-      $module_data = system_rebuild_module_data();
-      $module_list = $module_list ? array_combine($module_list, $module_list) : array();
-      if ($missing_modules = array_diff_key($module_list, $module_data)) {
-        // One or more of the given modules doesn't exist.
-        throw new MissingDependencyException(SafeMarkup::format('Unable to install modules %modules due to missing modules %missing.', array(
-          '%modules' => implode(', ', $module_list),
-          '%missing' => implode(', ', $missing_modules),
-        )));
-      }
+  protected function getModuleDataFromModuleList(&$module_list) {
+    $module_data = system_rebuild_module_data();
+    $module_list = $module_list ? array_combine($module_list, $module_list) : [];
+    if ($missing_modules = array_diff_key($module_list, $module_data)) {
+      // One or more of the given modules doesn't exist.
+      throw new MissingDependencyException(SafeMarkup::format('Unable to install modules %modules due to missing modules %missing.', array(
+        '%modules' => implode(', ', $module_list),
+        '%missing' => implode(', ', $missing_modules),
+      )));
+    }
+    return $module_data;
+  }
 
-      // Only process currently uninstalled modules.
-      $installed_modules = $extension_config->get('module') ?: array();
-      if (!$module_list = array_diff_key($module_list, $installed_modules)) {
-        // Nothing to do. All modules already installed.
-        return TRUE;
-      }
+  /**
+   * Find and add module dependencies to the list of modules to install.
+   *
+   * @param array $module_list
+   *   List of modules to install
+   *
+   * @return
+   *   Modified list.
+   */
+  protected function addDependenciesToModuleInstallList(array $module_list) {
+    $module_data = $this->getModuleDataFromModuleList($module_list);
+
+    // Only process currently uninstalled modules.
+    $installed_modules = $this->extensionConfig->get('module') ?: [];
+    $module_list = array_diff_key($module_list, $installed_modules);
+
+    if (count($module_list) > 0) {
 
       // Add dependencies to the list. The new modules will be processed as
       // the while loop continues.
@@ -130,180 +182,390 @@ public function install(array $module_list, $enable_dependencies = TRUE) {
       $module_list = array_keys($module_list);
     }
 
+    return $module_list;
+  }
+
+  /**
+   * Throws an exception if the module name is too long.
+   */
+  protected function checkModuleNameLength() {
+    // Throw an exception if the module name is too long.
+    if (strlen($this->module) > DRUPAL_EXTENSION_NAME_MAX_LENGTH) {
+      throw new ExtensionNameLengthException(format_string('Module name %name is over the maximum allowed length of @max characters.', array(
+        '%name' => $this->module,
+        '@max' => DRUPAL_EXTENSION_NAME_MAX_LENGTH,
+      )));
+    }
+  }
+
+  /**
+   * Checks the validity of the default configuration.
+   *
+   * This will throw exceptions if the configuration is not valid.
+   */
+  protected function configureModule() {
+    $this->configInstaller->checkConfigurationToInstall('module', $this->module);
+
+    // Save this data without checking schema. This is a performance
+    // improvement for module installation.
+    $this->extensionConfig
+      ->set("module.{$this->module}", 0)
+      ->set('module', module_config_sort($this->extensionConfig->get('module')))
+      ->save(TRUE);
+  }
+
+  /**
+   * Prepare the new module list, sorted by weight, including filenames.
+   *
+   * This list is used for both the ModuleHandler and DrupalKernel. It
+   * needs to be kept in sync between both. A DrupalKernel reboot or
+   * rebuild will automatically re-instantiate a new ModuleHandler that
+   * uses the new module list of the kernel. However, DrupalKernel does
+   * not cause any modules to be loaded.
+   * Furthermore, the currently active (fixed) module list can be
+   * different from the configured list of enabled modules. For all active
+   * modules not contained in the configured enabled modules, we assume a
+   * weight of 0.
+   */
+  protected function createNewModuleList() {
+    $current_module_filenames = $this->moduleHandler->getModuleList();
+    $current_modules = array_fill_keys(array_keys($current_module_filenames), 0);
+    $current_modules = module_config_sort(array_merge($current_modules, $this->extensionConfig->get('module')));
+    $module_filenames = [];
+    foreach ($current_modules as $name => $weight) {
+      if (isset($current_module_filenames[$name])) {
+        $module_filenames[$name] = $current_module_filenames[$name];
+      }
+      else {
+        $module_path = drupal_get_path('module', $name);
+        $pathname = "$module_path/$name.info.yml";
+        $filename = file_exists($module_path . "/$name.module") ? "$name.module" : NULL;
+        $module_filenames[$name] = new Extension($this->root, 'module', $pathname, $filename);
+      }
+    }
+    assert('count($module_filenames) >= count($current_module_filenames)',
+      'There should be at least as many modules as we started with.'
+    );
+    return $module_filenames;
+  }
+
+  /**
+   * Update the module handler in order to load the module's code.
+   *
+   * This allows the module to participate in hooks and its existence to
+   * be discovered by other modules. The current ModuleHandler instance is made
+   * obsolete with this kernel rebuild.
+   */
+  protected function updateModuleHandlerAndKernel( array $module_filenames) {
+
+    $this->moduleHandler->setModuleList($module_filenames);
+    $this->moduleHandler->load($this->module);
+    module_load_install($this->module);
+
+    // Clear the static cache of system_rebuild_module_data() to pick up the
+    // new module, since it merges the installation status of modules into
+    // its statically cached list.
+    drupal_static_reset('system_rebuild_module_data');
+
+    // Update the kernel to include it.
+    $this->updateKernel($module_filenames);
+  }
+
+  /**
+   * Determine the module schema version to use.
+   *
+   * Set the schema version to the number of the last update provided by
+   * the module, or the minimum core schema version. If the module has no
+   * current updates, but has some that were previously removed, set the version
+   * to the value of hook_update_last_removed().
+   */
+  protected function getModuleSchemaVersion() {
+    $version = \Drupal::CORE_MINIMUM_SCHEMA_VERSION;
+    $versions = drupal_get_schema_versions($this->module);
+    if ($versions) {
+      $version = max(max($versions), $version);
+    }
+
+    if ($last_removed = $this->moduleHandler->invoke($this->module, 'update_last_removed')) {
+      $version = max($version, $last_removed);
+    }
+
+    return $version;
+  }
+
+  /**
+   * Notify interested components that this module's entity types are new.
+   *
+   * For example, a SQL-based storage handler can use this as an
+   * opportunity to create the necessary database tables.
+   * @todo Clean this up in https://www.drupal.org/node/2350111.
+   */
+  protected function createEntities() {
+    $entity_manager = \Drupal::entityManager();
+    foreach ($entity_manager->getDefinitions() as $entity_type) {
+      if ($entity_type->getProvider() === $this->module) {
+        $entity_manager->onEntityTypeCreate($entity_type);
+      }
+    }
+  }
+
+  /**
+   * Install default configuration of the module.
+   */
+  protected function installDefaultConfiguration() {
+    if ($this->configInstaller->isSyncing()) {
+      $this->configInstaller
+        ->setSyncing(TRUE)
+        ->setSourceStorage($this->configInstaller->getSourceStorage());
+    }
+    $this->configInstaller->installDefaultConfig('module', $this->module);
+  }
+
+  /**
+   * Install a module
+   *
+   * @param string $module
+   *   Module name.
+   */
+  protected function installModule($module) {
+    $this->module = $module;
+    $this->checkModuleNameLength();
+    $this->configureModule();
+    $this->updateModuleHandlerAndKernel($this->createNewModuleList());
+
+    // Refresh the schema to include it.
+    drupal_get_schema(NULL, TRUE);
+
+    // Allow modules to react prior to the installation of a module.
+    $this->moduleHandler->invokeAll('module_preinstall', [$module]);
+
+    // Now install the module's schema if necessary.
+    drupal_install_schema($module);
+
+    // Clear plugin manager caches.
+    \Drupal::getContainer()->get('plugin.cache_clearer')->clearCachedDefinitions();
+
+    $this->createEntities();
+
+    $this->installDefaultConfiguration();
+
+    drupal_set_installed_schema_version($module, $this->getModuleSchemaVersion());
+
+    // file_get_stream_wrappers() needs to re-register Drupal's stream
+    // wrappers in case a module-provided stream wrapper is used later in
+    // the same request. In particular, this happens when installing Drupal
+    // via Drush, as the 'translations' stream wrapper is provided by
+    // Interface Translation module and is later used to import
+    // translations.
+    \Drupal::service('stream_wrapper_manager')->register();
+
+    $this->refreshThemes();
+
+    // Allow the module to perform install tasks.
+    $this->moduleHandler->invoke($module, 'install');
+
+    return $module;
+  }
+
+  /**
+   * Update the theme registry to include it.
+   */
+  protected function refreshThemes() {
+    drupal_theme_rebuild();
+
+    // Modules can alter theme info, so refresh theme data.
+    // @todo ThemeHandler cannot be injected into ModuleHandler, since that
+    //   causes a circular service dependency.
+    // @see https://drupal.org/node/2208429
+    \Drupal::service('theme_handler')->refreshInfo();
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function install(array $module_list, $enable_dependencies = TRUE) {
     // Required for module installation checks.
-    include_once $this->root . '/core/includes/install.inc';
+    require_once $this->root . '/core/includes/install.inc';
+    $this->configInstaller = \Drupal::service('config.installer');
+    $this->extensionConfig = \Drupal::configFactory()->getEditable('core.extension');
+    $this->configManager = \Drupal::service('config.manager');
+
+    // Prepare for a rollback if the incoming module code fails.
+    register_shutdown_function([$this, 'rollBackModuleInstall']);
 
-    /** @var \Drupal\Core\Config\ConfigInstaller $config_installer */
-    $config_installer = \Drupal::service('config.installer');
-    $sync_status = $config_installer->isSyncing();
-    if ($sync_status) {
-      $source_storage = $config_installer->getSourceStorage();
+    if ($enable_dependencies) {
+      $module_list = $this->addDependenciesToModuleInstallList($module_list);
     }
-    $modules_installed = array();
-    foreach ($module_list as $module) {
-      $enabled = $extension_config->get("module.$module") !== NULL;
-      if (!$enabled) {
-        // Throw an exception if the module name is too long.
-        if (strlen($module) > DRUPAL_EXTENSION_NAME_MAX_LENGTH) {
-          throw new ExtensionNameLengthException(format_string('Module name %name is over the maximum allowed length of @max characters.', array(
-            '%name' => $module,
-            '@max' => DRUPAL_EXTENSION_NAME_MAX_LENGTH,
-          )));
-        }
 
-        // Check the validity of the default configuration. This will throw
-        // exceptions if the configuration is not valid.
-        $config_installer->checkConfigurationToInstall('module', $module);
-
-        // Save this data without checking schema. This is a performance
-        // improvement for module installation.
-        $extension_config
-          ->set("module.$module", 0)
-          ->set('module', module_config_sort($extension_config->get('module')))
-          ->save(TRUE);
-
-        // Prepare the new module list, sorted by weight, including filenames.
-        // This list is used for both the ModuleHandler and DrupalKernel. It
-        // needs to be kept in sync between both. A DrupalKernel reboot or
-        // rebuild will automatically re-instantiate a new ModuleHandler that
-        // uses the new module list of the kernel. However, DrupalKernel does
-        // not cause any modules to be loaded.
-        // Furthermore, the currently active (fixed) module list can be
-        // different from the configured list of enabled modules. For all active
-        // modules not contained in the configured enabled modules, we assume a
-        // weight of 0.
-        $current_module_filenames = $this->moduleHandler->getModuleList();
-        $current_modules = array_fill_keys(array_keys($current_module_filenames), 0);
-        $current_modules = module_config_sort(array_merge($current_modules, $extension_config->get('module')));
-        $module_filenames = array();
-        foreach ($current_modules as $name => $weight) {
-          if (isset($current_module_filenames[$name])) {
-            $module_filenames[$name] = $current_module_filenames[$name];
-          }
-          else {
-            $module_path = drupal_get_path('module', $name);
-            $pathname = "$module_path/$name.info.yml";
-            $filename = file_exists($module_path . "/$name.module") ? "$name.module" : NULL;
-            $module_filenames[$name] = new Extension($this->root, 'module', $pathname, $filename);
-          }
-        }
+    if (count($module_list) > 0) {
 
-        // Update the module handler in order to load the module's code.
-        // This allows the module to participate in hooks and its existence to
-        // be discovered by other modules.
-        // The current ModuleHandler instance is obsolete with the kernel
-        // rebuild below.
-        $this->moduleHandler->setModuleList($module_filenames);
-        $this->moduleHandler->load($module);
-        module_load_install($module);
-
-        // Clear the static cache of system_rebuild_module_data() to pick up the
-        // new module, since it merges the installation status of modules into
-        // its statically cached list.
-        drupal_static_reset('system_rebuild_module_data');
-
-        // Update the kernel to include it.
-        $this->updateKernel($module_filenames);
-
-        // Refresh the schema to include it.
-        drupal_get_schema(NULL, TRUE);
-
-        // Allow modules to react prior to the installation of a module.
-        $this->moduleHandler->invokeAll('module_preinstall', array($module));
-
-        // Now install the module's schema if necessary.
-        drupal_install_schema($module);
-
-        // Clear plugin manager caches.
-        \Drupal::getContainer()->get('plugin.cache_clearer')->clearCachedDefinitions();
-
-        // Set the schema version to the number of the last update provided by
-        // the module, or the minimum core schema version.
-        $version = \Drupal::CORE_MINIMUM_SCHEMA_VERSION;
-        $versions = drupal_get_schema_versions($module);
-        if ($versions) {
-          $version = max(max($versions), $version);
+      $modules_installed = [];
+      foreach ($module_list as $module) {
+        if ($this->extensionConfig->get("module.$module") === NULL) {
+          $modules_installed[] = $this->installModule($module);
+          \Drupal::logger('system')->info('%module module installed.', ['%module' => $module]);
         }
+      }
 
-        // Notify interested components that this module's entity types are new.
-        // For example, a SQL-based storage handler can use this as an
-        // opportunity to create the necessary database tables.
-        // @todo Clean this up in https://www.drupal.org/node/2350111.
-        $entity_manager = \Drupal::entityManager();
-        foreach ($entity_manager->getDefinitions() as $entity_type) {
-          if ($entity_type->getProvider() == $module) {
-            $entity_manager->onEntityTypeCreate($entity_type);
-          }
-        }
+      // If any modules were newly installed, invoke hook_modules_installed().
+      if (!empty($modules_installed)) {
+        \Drupal::service('router.builder')->setRebuildNeeded();
+        $this->moduleHandler->invokeAll('modules_installed', [$modules_installed]);
+      }
+    }
 
-        // Install default configuration of the module.
-        $config_installer = \Drupal::service('config.installer');
-        if ($sync_status) {
-          $config_installer
-            ->setSyncing(TRUE)
-            ->setSourceStorage($source_storage);
-        }
-        \Drupal::service('config.installer')->installDefaultConfig('module', $module);
+    // Cancel rollback.
+    $this->rollBackRequired = FALSE;
 
-        // If the module has no current updates, but has some that were
-        // previously removed, set the version to the value of
-        // hook_update_last_removed().
-        if ($last_removed = $this->moduleHandler->invoke($module, 'update_last_removed')) {
-          $version = max($version, $last_removed);
+    return TRUE;
+  }
+
+  /**
+   * Clean up all entity bundles of module being uninstalled.
+   *
+   * Clean up all entity bundles (including fields) of every entity type
+   * provided by the module that is being uninstalled.
+   * @todo Clean this up in https://www.drupal.org/node/2350111.
+   */
+  protected function deleteEntityBundles() {
+    $entity_manager = \Drupal::entityManager();
+    foreach ($entity_manager->getDefinitions() as $entity_type_id => $entity_type) {
+      if ($entity_type->getProvider() == $this->module) {
+        foreach (array_keys($entity_manager->getBundleInfo($entity_type_id)) as $bundle) {
+          $entity_manager->onBundleDelete($bundle, $entity_type_id);
         }
-        drupal_set_installed_schema_version($module, $version);
+      }
+    }
+  }
 
-        // Record the fact that it was installed.
-        $modules_installed[] = $module;
+  /**
+   * Notify components that this module's entity types are being deleted.
+   *
+   * For example, a SQL-based storage handler can use this as an
+   * opportunity to drop the corresponding database tables.
+   * @todo Clean this up in https://www.drupal.org/node/2350111.
+   */
+  protected function deleteEntityTypes() {
+    $entity_manager = \Drupal::entityManager();
+    foreach ($entity_manager->getDefinitions() as $entity_type) {
+      if ($entity_type->getProvider() == $this->module) {
+        $entity_manager->onEntityTypeDelete($entity_type);
+      }
+    }
+  }
 
-        // file_get_stream_wrappers() needs to re-register Drupal's stream
-        // wrappers in case a module-provided stream wrapper is used later in
-        // the same request. In particular, this happens when installing Drupal
-        // via Drush, as the 'translations' stream wrapper is provided by
-        // Interface Translation module and is later used to import
-        // translations.
-        \Drupal::service('stream_wrapper_manager')->register();
+  /**
+   * Update the module handler to remove the module.
+   *
+   * The current ModuleHandler instance is obsolete with the kernel rebuild.
+   */
+  protected function unregisterFromModuleHandler() {
+    $module_filenames = $this->moduleHandler->getModuleList();
+    unset($module_filenames[$this->module]);
+    $this->moduleHandler->setModuleList($module_filenames);
+    return $module_filenames;
+  }
 
-        // Update the theme registry to include it.
-        drupal_theme_rebuild();
+  /**
+   * Remove any potential cache bins provided by the module.
+   */
+  protected function uncacheModule() {
+    $this->removeCacheBins($this->module);
 
-        // Modules can alter theme info, so refresh theme data.
-        // @todo ThemeHandler cannot be injected into ModuleHandler, since that
-        //   causes a circular service dependency.
-        // @see https://drupal.org/node/2208429
-        \Drupal::service('theme_handler')->refreshInfo();
+    // Clear the static cache of system_rebuild_module_data() to pick up the
+    // new module, since it merges the installation status of modules into
+    // its statically cached list.
+    drupal_static_reset('system_rebuild_module_data');
 
-        // Allow the module to perform install tasks.
-        $this->moduleHandler->invoke($module, 'install');
+    // Clear plugin manager caches.
+    \Drupal::getContainer()->get('plugin.cache_clearer')->clearCachedDefinitions();
+  }
 
-        // Record the fact that it was installed.
-        \Drupal::logger('system')->info('%module module installed.', array('%module' => $module));
+  /**
+   * Uninstall a partially installed module if necessary.
+   */
+  public function rollBackModuleInstall() {
+    if ($this->rollBackRequired) {
+      $this->deleteEntityBundles();
+      module_load_install($this->module);
+
+      // Remove all configuration belonging to the module.
+   //   $this->configManager->uninstall('module', $this->module);
+      $this->deleteEntityTypes();
+
+      // Remove the schema.
+      drupal_uninstall_schema($this->module);
+
+      // Remove the module's entry from the config. Don't check schema when
+      // uninstalling a module since we are only clearing a key.
+      try {
+        \Drupal::configFactory()->getEditable('core.extension')->clear("module.{$this->module}")->save(TRUE);
       }
-    }
+      catch ( StorageException $e ) {
+
+      }
+      // Update the kernel to exclude the uninstalled modules.
+  //    $this->updateKernel($this->unregisterFromModuleHandler());
+      $this->uncacheModule();
+      $this->refreshThemes();
 
-    // If any modules were newly installed, invoke hook_modules_installed().
-    if (!empty($modules_installed)) {
-      \Drupal::service('router.builder')->setRebuildNeeded();
-      $this->moduleHandler->invokeAll('modules_installed', array($modules_installed));
+      $this->removeModuleSchema();
     }
+  }
 
-    return TRUE;
+  /**
+   * Uninstall a single module.
+   *
+   * @param string $module
+   *   Module Name.
+   */
+  protected function uninstallModule($module) {
+    $this->module = $module;
+    $this->deleteEntityBundles();
+    // Allow modules to react prior to the uninstallation of a module.
+    $this->moduleHandler->invokeAll('module_preuninstall', array($this->module));
+
+    // Uninstall the module.
+    module_load_install($this->module);
+    $this->moduleHandler->invoke($this->module, 'uninstall');
+
+    // Remove all configuration belonging to the module.
+    \Drupal::service('config.manager')->uninstall('module', $this->module);
+    $this->deleteEntityTypes();
+
+    // Remove the schema.
+    drupal_uninstall_schema($this->module);
+
+    // Remove the module's entry from the config. Don't check schema when
+    // uninstalling a module since we are only clearing a key.
+    \Drupal::configFactory()->getEditable('core.extension')->clear("module.{$this->module}")->save(TRUE);
+
+    // Update the kernel to exclude the uninstalled modules.
+    $this->updateKernel($this->unregisterFromModuleHandler());
+    $this->uncacheModule();
+    $this->refreshThemes();
+    $this->removeModuleSchema();
+  }
+
+  protected function removeModuleSchema() {
+    $schema_store = \Drupal::keyValue('system.schema');
+    $schema_store->delete($this->module);
   }
 
   /**
    * {@inheritdoc}
    */
   public function uninstall(array $module_list, $uninstall_dependents = TRUE) {
+    $this->extensionConfig = \Drupal::configFactory()->getEditable('core.extension');
+
     // Get all module data so we can find dependencies and sort.
-    $module_data = system_rebuild_module_data();
-    $module_list = $module_list ? array_combine($module_list, $module_list) : array();
-    if (array_diff_key($module_list, $module_data)) {
-      // One or more of the given modules doesn't exist.
+    try {
+      $module_data = $this->getModuleDataFromModuleList($module_list);
+    } catch (MissingDependencyException $e) {
       return FALSE;
     }
 
-    $extension_config = \Drupal::configFactory()->getEditable('core.extension');
-    $installed_modules = $extension_config->get('module') ?: array();
+    $installed_modules = $this->extensionConfig->get('module') ?: array();
     if (!$module_list = array_intersect_key($module_list, $installed_modules)) {
       // Nothing to do. All modules already uninstalled.
       return TRUE;
@@ -351,81 +613,10 @@ public function uninstall(array $module_list, $uninstall_dependents = TRUE) {
     // the module already, which means that it might be loaded, but not
     // necessarily installed.
     foreach ($module_list as $module) {
-
-      // Clean up all entity bundles (including fields) of every entity type
-      // provided by the module that is being uninstalled.
-      // @todo Clean this up in https://www.drupal.org/node/2350111.
-      $entity_manager = \Drupal::entityManager();
-      foreach ($entity_manager->getDefinitions() as $entity_type_id => $entity_type) {
-        if ($entity_type->getProvider() == $module) {
-          foreach (array_keys($entity_manager->getBundleInfo($entity_type_id)) as $bundle) {
-            $entity_manager->onBundleDelete($bundle, $entity_type_id);
-          }
-        }
-      }
-
-      // Allow modules to react prior to the uninstallation of a module.
-      $this->moduleHandler->invokeAll('module_preuninstall', array($module));
-
-      // Uninstall the module.
-      module_load_install($module);
-      $this->moduleHandler->invoke($module, 'uninstall');
-
-      // Remove all configuration belonging to the module.
-      \Drupal::service('config.manager')->uninstall('module', $module);
-
-      // Notify interested components that this module's entity types are being
-      // deleted. For example, a SQL-based storage handler can use this as an
-      // opportunity to drop the corresponding database tables.
-      // @todo Clean this up in https://www.drupal.org/node/2350111.
-      foreach ($entity_manager->getDefinitions() as $entity_type) {
-        if ($entity_type->getProvider() == $module) {
-          $entity_manager->onEntityTypeDelete($entity_type);
-        }
-      }
-
-      // Remove the schema.
-      drupal_uninstall_schema($module);
-
-      // Remove the module's entry from the config. Don't check schema when
-      // uninstalling a module since we are only clearing a key.
-      \Drupal::configFactory()->getEditable('core.extension')->clear("module.$module")->save(TRUE);
-
-      // Update the module handler to remove the module.
-      // The current ModuleHandler instance is obsolete with the kernel rebuild
-      // below.
-      $module_filenames = $this->moduleHandler->getModuleList();
-      unset($module_filenames[$module]);
-      $this->moduleHandler->setModuleList($module_filenames);
-
-      // Remove any potential cache bins provided by the module.
-      $this->removeCacheBins($module);
-
-      // Clear the static cache of system_rebuild_module_data() to pick up the
-      // new module, since it merges the installation status of modules into
-      // its statically cached list.
-      drupal_static_reset('system_rebuild_module_data');
-
-      // Clear plugin manager caches.
-      \Drupal::getContainer()->get('plugin.cache_clearer')->clearCachedDefinitions();
-
-      // Update the kernel to exclude the uninstalled modules.
-      $this->updateKernel($module_filenames);
-
-      // Update the theme registry to remove the newly uninstalled module.
-      drupal_theme_rebuild();
-
-      // Modules can alter theme info, so refresh theme data.
-      // @todo ThemeHandler cannot be injected into ModuleHandler, since that
-      //   causes a circular service dependency.
-      // @see https://drupal.org/node/2208429
-      \Drupal::service('theme_handler')->refreshInfo();
-
+      $this->uninstallModule($module);
       \Drupal::logger('system')->info('%module module uninstalled.', array('%module' => $module));
-
-      $schema_store = \Drupal::keyValue('system.schema');
-      $schema_store->delete($module);
     }
+
     \Drupal::service('router.builder')->setRebuildNeeded();
     drupal_get_installed_schema_version(NULL, TRUE);
 
diff --git a/core/modules/system/src/Tests/Module/InstallTest.php b/core/modules/system/src/Tests/Module/InstallTest.php
index 1068b60..38a804b 100644
--- a/core/modules/system/src/Tests/Module/InstallTest.php
+++ b/core/modules/system/src/Tests/Module/InstallTest.php
@@ -80,4 +80,34 @@ public function testModuleNameLength() {
     }
   }
 
+  /**
+   * Tests enabling a bugged module.
+   *
+   * The module to be enabled will produce a PHP SynatxError during install.
+   * It must not break the website.
+   */
+  public function testEnableBadModule() {
+
+    // Create and log as admin user.
+    $this->adminUser = $this->drupalCreateUser(array('administer modules'));
+    $this->drupalLogin($this->adminUser);
+
+    // Enable bad_module module.
+    $edit['modules[Testing][bad_module][enable]'] = TRUE;
+    $this->drupalPostForm('admin/modules', $edit, t('Save configuration'));
+
+    // Check the syntax error occured.
+    $this->assertText("PHP syntax exception !");
+
+    // Check that the module is not enabled.
+    $this->drupalGet('admin/modules');
+    $this->assertNoFieldChecked('edit-modules-testing-bad-module-enable', 'The module bad_module is not enabled.');
+    $this->drupalGet('admin/modules/uninstall');
+    $this->assertNoText('Bad module', 'The module bad_module is not in module uninstall list.');
+
+    // The exceptions are expected. Do not interpret them as a test failure.
+    // Not using File API; a potential error must trigger a PHP warning.
+    unlink(\Drupal::root() . '/' . $this->siteDirectory . '/error.log');
+  }
+
 }
diff --git a/core/modules/system/tests/modules/bad_module/bad_module.info.yml b/core/modules/system/tests/modules/bad_module/bad_module.info.yml
new file mode 100644
index 0000000..96e71e0
--- /dev/null
+++ b/core/modules/system/tests/modules/bad_module/bad_module.info.yml
@@ -0,0 +1,6 @@
+name: 'Bad module'
+type: module
+description: 'Don&#39;t activate me, I have a PHP syntax error.'
+package: Testing
+version: VERSION
+core: 8.x
diff --git a/core/modules/system/tests/modules/bad_module/bad_module.module b/core/modules/system/tests/modules/bad_module/bad_module.module
new file mode 100644
index 0000000..d74ebf2
--- /dev/null
+++ b/core/modules/system/tests/modules/bad_module/bad_module.module
@@ -0,0 +1,13 @@
+<?php
+
+/**
+ * Implements hook_install().
+ */
+function bad_module_install() {
+  // Prevent test from collecting errors.
+  define('SIMPLETEST_COLLECT_ERRORS', FALSE);
+  // This simulates a PHP syntax error or whatever unexpected behavior that
+  // might happen during the install process of a module.
+  // @see \Drupal\system\Tests\Module\InstallTest
+  throw new Exception("PHP syntax exception !");
+}
