Clear cache programmatically in Drupal 8

Programmatically using PHP

just use this method where you want to clear the cache:

drupal_flush_all_caches();

there is also other methods to clear caches using:

By Admin UI

Navigate to /admin/config/development/performance and ​click the button "Clear all caches".


By Drush

drush cache-rebuild

//or you can use

drush cr


By update.php

Run update.php (/update.php) is another way of clearing the cache.


By SQL

Delete all data inside  tables that start with "cache_" like this:

TRUNCATE cache_config;

TRUNCATE cache_container;

TRUNCATE cache_data;

TRUNCATE cache_default;

TRUNCATE cache_discovery;

TRUNCATE cache_dynamic_page_cache;

TRUNCATE cache_entity;

TRUNCATE cache_menu;

TRUNCATE cache_render;

TRUNCATE cache_toolbar;


Other Methods:


$variables['#cache']['max-age'] = 0;


\Drupal::service('page_cache_kill_switch')->trigger();


cache_clear_all() // For Drupal-7


drupal_flush_all_caches() // For Drupal-8

PHP

 

If you want to clear specific cache like render cache then you can run the following code:


\Drupal::service('cache.render')->invalidateAll();

PHP

 


If you want to clear specific cache like route cache then you can run the following code:


\Drupal::service("router.builder")->rebuild();

No comments:

Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...