How to clear cache programmatically in Drupal?

How to clear cache programmatically in Drupal?

In Drupal, you can clear the cache programmatically using the Drupal APIs. Here are a few different ways to clear the cache depending on your specific requirements.

Method 1: Clear All Caches

You can clear all caches by invoking the cache rebuild command programmatically. This is equivalent to running drush cache-rebuild.

use Drupal\Core\Cache\Cache; // Rebuild all caches. drupal_flush_all_caches(); 

Method 2: Clear Specific Cache Bins

If you need to clear a specific cache bin, you can use the cache service. Here's how to clear specific cache bins:

use Drupal\Core\Cache\Cache; $cache_bins = [ 'render', 'dynamic_page_cache', 'page', 'data', 'bootstrap', 'config', ]; foreach ($cache_bins as $bin) { \Drupal::cache($bin)->deleteAll(); } 

Method 3: Clear Cache Tags

You can also clear caches by specific cache tags. This is useful if you want to invalidate caches related to specific content or configuration.

use Drupal\Core\Cache\Cache; // Invalidate caches tagged with 'node_list'. Cache::invalidateTags(['node_list']); // Invalidate caches tagged with multiple tags. Cache::invalidateTags(['node:1', 'node:2']); 

Method 4: Clear View Cache

If you need to clear caches for a specific view, you can do so by invoking the view and invalidating its cache.

$view = \Drupal\views\Views::getView('view_machine_name'); if (is_object($view)) { $view->storage->invalidateCache(); } 

Method 5: Clear Custom Cache Bins

If you have custom cache bins defined, you can clear them similarly:

use Drupal\Core\Cache\CacheBackendInterface; // Clear a custom cache bin. \Drupal::service('cache.custom_bin')->deleteAll(); 

Putting It All Together

You can create a custom module or include this code in an existing custom module to clear the cache programmatically. Here's an example of a custom module implementation:

  1. Create a custom module (e.g., custom_clear_cache).
  2. Create a file custom_clear_cache.info.yml:
name: 'Custom Clear Cache' type: module description: 'Provides a way to clear cache programmatically.' core_version_requirement: ^8 || ^9 package: Custom dependencies: [] 
  1. Create a file custom_clear_cache.module:
<?php /** * Implements hook_help(). */ function custom_clear_cache_help($route_name, $route_match) { switch ($route_name) { case 'help.page.custom_clear_cache': return '<p>' . t('Provides a way to clear cache programmatically.') . '</p>'; } } /** * Clear all caches programmatically. */ function custom_clear_cache_all() { drupal_flush_all_caches(); } /** * Clear specific cache bins. */ function custom_clear_specific_caches() { $cache_bins = [ 'render', 'dynamic_page_cache', 'page', 'data', 'bootstrap', 'config', ]; foreach ($cache_bins as $bin) { \Drupal::cache($bin)->deleteAll(); } } /** * Clear caches by tags. */ function custom_clear_cache_tags() { Cache::invalidateTags(['node_list', 'config:user.role.administrator']); } 

You can then invoke these functions from anywhere in your custom module or through a custom route/controller as needed. This setup provides a structured way to clear various caches programmatically within your Drupal site.

Examples

  1. "How to clear all caches programmatically in Drupal 8/9"

    Description: Clear all caches programmatically using Drupal's Cache API.

    Code:

    use Drupal\Core\Cache\Cache; // Clear all caches Cache::invalidateTags(['rendered']); \Drupal::service('cache.bootstrap')->deleteAll(); \Drupal::service('cache.config')->deleteAll(); \Drupal::service('cache.discovery')->deleteAll(); \Drupal::service('cache.data')->deleteAll(); \Drupal::service('cache.default')->deleteAll(); \Drupal::service('cache.entity')->deleteAll(); \Drupal::service('cache.menu')->deleteAll(); \Drupal::service('cache.render')->deleteAll(); 
  2. "How to clear specific cache bins programmatically in Drupal 8/9"

    Description: Clear specific cache bins such as config, data, or render cache.

    Code:

    // Clear the render cache \Drupal::service('cache.render')->deleteAll(); // Clear the config cache \Drupal::service('cache.config')->deleteAll(); // Clear the data cache \Drupal::service('cache.data')->deleteAll(); 
  3. "How to clear cache tags programmatically in Drupal 8/9"

    Description: Invalidate specific cache tags to clear related cached content.

    Code:

    use Drupal\Core\Cache\Cache; // Invalidate specific cache tags Cache::invalidateTags(['node:1', 'taxonomy_term:2']); 
  4. "How to clear views cache programmatically in Drupal 8/9"

    Description: Clear cache for views programmatically using Views Cache Tags.

    Code:

    use Drupal\views\Views; // Load the view $view = Views::getView('my_view'); if ($view) { // Clear cache for this view $view->invalidateCaches(); } 
  5. "How to clear entity cache programmatically in Drupal 8/9"

    Description: Invalidate cache for a specific entity type.

    Code:

    use Drupal\Core\Cache\Cache; // Invalidate cache for all nodes Cache::invalidateTags(['node_list']); // Invalidate cache for a specific node Cache::invalidateTags(['node:1']); 
  6. "How to clear theme registry cache programmatically in Drupal 8/9"

    Description: Clear theme registry cache programmatically.

    Code:

    // Clear the theme registry cache \Drupal::service('cache.discovery')->deleteAll(); 
  7. "How to clear menu cache programmatically in Drupal 8/9"

    Description: Clear the menu cache programmatically.

    Code:

    // Clear the menu cache \Drupal::service('cache.menu')->deleteAll(); 
  8. "How to clear block cache programmatically in Drupal 8/9"

    Description: Clear cache for a specific block or all blocks.

    Code:

    use Drupal\Core\Cache\Cache; // Invalidate cache for a specific block Cache::invalidateTags(['block_view:block_name']); 
  9. "How to clear route cache programmatically in Drupal 8/9"

    Description: Clear route cache programmatically.

    Code:

    // Clear the route cache \Drupal::service('router.builder')->rebuild(); 
  10. "How to clear the Twig template cache programmatically in Drupal 8/9"

    Description: Clear the Twig template cache programmatically.

    Code:

    // Clear the Twig cache \Drupal::service('twig')->invalidateTemplateCache(); 

More Tags

preferences protractor-net transpiler mediatr monitor sim-card webviewclient navigationbar php-openssl beanshell

More Programming Questions

More Trees & Forestry Calculators

More Everyday Utility Calculators

More Organic chemistry Calculators

More Stoichiometry Calculators