diff --git a/core/includes/common.inc b/core/includes/common.inc
index b6582df851deab6bcd923ede387c9db2d25ca585..19773320dba65a0934bb2f05fbd31ddd74de274d 100644
--- a/core/includes/common.inc
+++ b/core/includes/common.inc
@@ -550,12 +550,15 @@ function drupal_flush_all_caches() {
   // sufficient, since the list of enabled modules might have been adjusted
   // above due to changed code.
   $files = [];
+  $modules = [];
   foreach ($module_data as $name => $extension) {
     if ($extension->status) {
       $files[$name] = $extension;
+      $modules[$name] = $extension->weight;
     }
   }
-  \Drupal::service('kernel')->updateModules($module_handler->getModuleList(), $files);
+  $modules = module_config_sort($modules);
+  \Drupal::service('kernel')->updateModules($modules, $files);
   // New container, new module handler.
   $module_handler = \Drupal::moduleHandler();
 
diff --git a/core/tests/Drupal/KernelTests/Core/Common/DrupalFlushAllCachesTest.php b/core/tests/Drupal/KernelTests/Core/Common/DrupalFlushAllCachesTest.php
new file mode 100644
index 0000000000000000000000000000000000000000..0953c05a4c7c84e4ef4d07bb2b233f4ce31b2ef6
--- /dev/null
+++ b/core/tests/Drupal/KernelTests/Core/Common/DrupalFlushAllCachesTest.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace Drupal\KernelTests\Core\Common;
+
+use Drupal\KernelTests\KernelTestBase;
+
+/**
+ * @covers ::drupal_flush_all_caches
+ * @group Common
+ */
+class DrupalFlushAllCachesTest extends KernelTestBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $modules = ['system'];
+
+  /**
+   * Tests that drupal_flush_all_caches() uses core.extension properly.
+   */
+  public function testDrupalFlushAllCachesModuleList() {
+    $core_extension = \Drupal::configFactory()->getEditable('core.extension');
+    $module = $core_extension->get('module');
+    $module['system_test'] = -10;
+    $core_extension->set('module', module_config_sort($module))->save();
+
+    drupal_flush_all_caches();
+
+    $this->assertSame(['system_test', 'system'], array_keys($this->container->getParameter('container.modules')));
+  }
+
+}