Skip to content

Instantly share code, notes, and snippets.

@labboy0276
Last active May 6, 2023 14:18
Show Gist options
  • Save labboy0276/687764dd8a6254656faf4e992daa756a to your computer and use it in GitHub Desktop.
Save labboy0276/687764dd8a6254656faf4e992daa756a to your computer and use it in GitHub Desktop.
Drupal 8/9/10 Core Cache Rebuild
{
"name": "drupal/YOUR_MODULE",
"type": "drupal-module",
"description": "YOUR_MODULE System Tweaks",
"extra": {
"drush": {
"services": {
"drush.services.yml": "^9 || ^10 || ^11"
}
}
}
}
services:
YOUR_MODULE.command_one:
class: \Drupal\YOUR_MODULE\Commands\RebuildCache
tags:
- { name: drush.command }
<?php
namespace Drupal\YOUR_MODULE\Commands;
use Drush\Commands\DrushCommands;
/**
* Rebuild the cache mechanism via drush.
*/
class RebuildCache extends DrushCommands {
/**
* Rebuild the cache mechanism via drush.
*
* @command custom:cache-rebuild
* @aliases ccrb
* @usage custom:cache-rebuild
*/
public function cacheRebuild() {
// Flush all the caches.
drupal_flush_all_caches();
// Handles clearing the kernel containers.
$kernel = \Drupal::service('kernel');
$kernel->invalidateContainer();
$kernel->rebuildContainer();
// Disable recording of cached pages.
\Drupal::service('page_cache_kill_switch')->trigger();
// APCU Clear if it exists.
if (function_exists('apcu_clear_cache')) {
apcu_clear_cache();
}
$this->output()->writeln('Core Cache rebuild complete.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment