diff --git a/core/includes/common.inc b/core/includes/common.inc
index 31492ed..367c3ba 100644
--- a/core/includes/common.inc
+++ b/core/includes/common.inc
@@ -13,6 +13,7 @@
 use Drupal\Component\Utility\Html;
 use Drupal\Component\Utility\SortArray;
 use Drupal\Component\Utility\UrlHelper;
+use Drupal\Core\DrupalKernel;
 use Drupal\Core\Cache\Cache;
 use Drupal\Core\Render\Markup;
 use Drupal\Core\Render\Renderer;
@@ -21,6 +22,7 @@
 use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
 use Drupal\Core\Render\BubbleableMetadata;
 use Drupal\Core\Render\Element;
+use Symfony\Component\HttpFoundation\Request;
 
 /**
  * @defgroup php_wrappers PHP wrapper functions
@@ -1076,11 +1078,27 @@ function element_info_property($type, $property_name, $default = NULL) {
  *   requests.
  */
 function drupal_flush_all_caches() {
+  // Invalidate the container.
+  // Bootstrap up to where caches exist and clear them.
+  $request = Request::createFromGlobals();
+  $kernel = new DrupalKernel('prod', require __DIR__ . '/../../autoload.php');
+  $kernel->setSitePath(DrupalKernel::findSitePath($request));
+
+  // Invalidate the container.
+  $kernel->invalidateContainer();
+  $kernel->boot();
+  $kernel->preHandle($request);
+
+  // Disable recording of cached pages.
+  \Drupal::service('page_cache_kill_switch')->trigger();
+
   $module_handler = \Drupal::moduleHandler();
+
   // Flush all persistent caches.
   // This is executed based on old/previously known information, which is
   // sufficient, since new extensions cannot have any primed caches yet.
   $module_handler->invokeAll('cache_flush');
+
   foreach (Cache::getBins() as $service_id => $cache_backend) {
     $cache_backend->deleteAll();
   }
@@ -1093,17 +1111,16 @@ function drupal_flush_all_caches() {
   // Reset all static caches.
   drupal_static_reset();
 
-  // Invalidate the container.
-  \Drupal::service('kernel')->invalidateContainer();
-
   // Wipe the Twig PHP Storage cache.
   PhpStorageFactory::get('twig')->deleteAll();
 
   // Rebuild module and theme data.
   $module_data = system_rebuild_module_data();
+
   /** @var \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler */
   $theme_handler = \Drupal::service('theme_handler');
   $theme_handler->refreshInfo();
+
   // In case the active theme gets requested later in the same request we need
   // to reset the theme manager.
   \Drupal::theme()->resetActiveTheme();
@@ -1118,7 +1135,9 @@ function drupal_flush_all_caches() {
       $files[$name] = $extension;
     }
   }
+
   \Drupal::service('kernel')->updateModules($module_handler->getModuleList(), $files);
+
   // New container, new module handler.
   $module_handler = \Drupal::moduleHandler();
 
diff --git a/core/includes/utility.inc b/core/includes/utility.inc
index 2bb6507..9ae1ee7 100644
--- a/core/includes/utility.inc
+++ b/core/includes/utility.inc
@@ -5,12 +5,11 @@
  * Miscellaneous functions.
  */
 
-use Drupal\Core\PhpStorage\PhpStorageFactory;
-use Drupal\Core\Cache\Cache;
-use Drupal\Core\DrupalKernel;
 use Symfony\Component\HttpFoundation\Request;
 use Composer\Autoload\ClassLoader;
 
+include __DIR__ . '/common.inc';
+
 /**
  * Rebuilds all caches even when Drupal itself does not work.
  *
@@ -28,26 +27,6 @@ function drupal_rebuild(ClassLoader $class_loader, Request $request) {
   restore_error_handler();
   restore_exception_handler();
 
-  // Force kernel to rebuild php cache.
-  PhpStorageFactory::get('twig')->deleteAll();
-
-  // Bootstrap up to where caches exist and clear them.
-  $kernel = new DrupalKernel('prod', $class_loader);
-  $kernel->setSitePath(DrupalKernel::findSitePath($request));
-
-  // Invalidate the container.
-  $kernel->invalidateContainer();
-
-  // Prepare a NULL request.
-  $kernel->prepareLegacyRequest($request);
-
-  foreach (Cache::getBins() as $bin) {
-    $bin->deleteAll();
-  }
-
-  // Disable recording of cached pages.
-  \Drupal::service('page_cache_kill_switch')->trigger();
-
   drupal_flush_all_caches();
 
   // Restore Drupal's error and exception handlers.
