diff --git a/core/rebuild.php b/core/rebuild.php
new file mode 100644
index 0000000..9ecf17d
--- /dev/null
+++ b/core/rebuild.php
@@ -0,0 +1,34 @@
+<?php
+
+/**
+ * @file
+ * Rebuilds all Drupal caches even when Drupal itself does not work.
+ *
+ * Needs a token query argument which can be calculated using the
+ * scripts/rebuild_token_calculator.sh script.
+ */
+
+use Drupal\Component\PhpStorage\PhpStorageFactory;
+use Drupal\Core\Cache\CacheFactory;
+use Drupal\Component\Utility\Crypt;
+
+// Change the directory to the Drupal root.
+chdir('..');
+
+require_once dirname(__DIR__) . '/core/includes/bootstrap.inc';
+drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
+if (isset($_GET['token']) && isset($_GET['timestamp']) && REQUEST_TIME - $_GET['timestamp'] < 300 && $_GET['token'] === ($_GET['timestamp'], $GLOBALS['drupal_hash_salt'])) {
+  // drupal_bootstrap(DRUPAL_BOOTSTRAP_KERNEL) will build a new kernel.
+  PhpStorageFactory::get('service_container')->deleteAll();
+  PhpStorageFactory::get('twig')->deleteAll();
+  $GLOBALS['conf']['system.performance']['cache.page.use_internal'] = FALSE;
+  // Bootstrap up to where caches exist and clear them.
+  drupal_bootstrap(DRUPAL_BOOTSTRAP_PAGE_CACHE);
+  foreach (CacheFactory::getBackends() as $bin => $backend) {
+    cache($bin)->deleteAll();
+  }
+  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
+  drupal_flush_all_caches();
+  drupal_set_message('Rebuild complete.');
+}
+header('Location: ' . $GLOBALS['base_url']);
diff --git a/core/scripts/rebuild_token_calculator.sh b/core/scripts/rebuild_token_calculator.sh
new file mode 100755
index 0000000..2369cd8
--- /dev/null
+++ b/core/scripts/rebuild_token_calculator.sh
@@ -0,0 +1,20 @@
+#!/usr/bin/env php
+<?php
+
+/**
+ * @file
+ * Command line token calculator for rebuild.php.
+ */
+
+require_once __DIR__ . '/../vendor/autoload.php';
+
+use Drupal\Component\Utility\Crypt;
+
+if (PHP_SAPI !== 'cli') {
+  exit;
+}
+
+$timestamp = time();
+$token = Crypt::hmacBase64($timestamp, $drupal_hash_salt);
+
+print "timestamp=$timestamp&token=$token\n";
