Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

in this tuto, I'll show you the methods for clearing or rebuilding Drupal 8 cache programmatically  and using admin UI, update.php, drush, SQL, ...

Programmatically using PHP

juste 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

 

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

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

 

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

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

 

 

Comments

Submitted by Jozef (not verified) on Mon, 03/22/2021 - 03:41 Permalink

I am Using Drupal 8 I have Taxonomy Person with custom defined attribute FirstName I have a custom module family_tree that has a function Show_person that shows 1 Taxonomy Person term. Action Sequence A: call url: /family-tree/show-person/2 (shows FirstName = Joe) call url: /taxonomy/term/2/edit (changing FirstName = Frank) call url: /family-tree/show-person/2 (and it shows the OLD FirstName = Joe) Action Sequence B: call url: /family-tree/show-person/2 (shows FirstName = Joe) call url: /taxonomy/term/2/edit (changing FirstName = Frank) go manually to /admin/config/development/performance and click „Clear All caches“ call url: /family-tree/show-person/2 (and it shows the NEW FirstName = Frank) Action Sequence C: Inside the custom module family_tree‘s function Show_person, just before calling $personObject = \Drupal::entityTypeManager()-> getStorage('taxonomy_term')-> load($person->tid);, I inserted drupal_flush_all_caches(); And then: call url: /family-tree/show-person/2 (shows FirstName = Joe, before showing, drupal_flush_all_caches(); was called) call url: /taxonomy/term/2/edit (changing FirstName = Frank) call url: /family-tree/show-person/2 (and it still shows the OLD FirstName = Joe, before showing, drupal_flush_all_caches(); was called) Action Sequence D: The same as Sequence C, but instead of drupal_flush_all_caches(); I called $result = db_query("TRUNCATE cache_config"); $result = db_query("TRUNCATE cache_container"); $result = db_query("TRUNCATE cache_data"); $result = db_query("TRUNCATE cache_default"); $result = db_query("TRUNCATE cache_discovery"); $result = db_query("TRUNCATE cache_dynamic_page_cache"); $result = db_query("TRUNCATE cache_entity"); $result = db_query("TRUNCATE cache_menu"); $result = db_query("TRUNCATE cache_render"); $result = db_query("TRUNCATE cache_toolbar"); Can you suggest, please, what could be the reason for my problem, i.e. that manual „Clear All Cashes“ works and programmatical calls do not work for me? Thank you. Jozef
Submitted by jozef (not verified) on Tue, 03/23/2021 - 08:18 Permalink

How can I be sure that I apply this method in the right place? When I do it in 3 steps: 1) Modify entity (taxonomy term) 2) Manually calling "Clear all caches" 3) Show entity (taxonomy term) in a custom module by calling service function Everything is fine. When I do it in 2 steps 1) Modify entity (taxonomy term) 2) Show entity (taxonomy term) in a custom module by calling service function, but before calling service function I call drupal_flush_all_caches() then the "Show" function shows the old content of the Entity... Is it possible that the Drupal Services use some cashes before they are called? That would explain this problem...
Submitted by Jozef (not verified) on Tue, 03/23/2021 - 08:26 Permalink

or, should I call drupal_flush_all_caches() in a call back function after saving the entity changes? But, in general, is it not strange that if I change the content of entity in one step, I cannot see the changes in the second step? That I have to Clear All cashes in between?
Submitted by jozef (not verified) on Mon, 04/05/2021 - 07:47 Permalink

I managed to call drupal_flush_all_caches() from a proper place, from hook_node_update($node). However, I would need only flush the cache for a specific url, e.g. mydomain/mymodule/myfunction/{node_id}. I spent a couple of hours researching, but found only a way how to do it in Drupal 7. Can I flush the cache for a specific url in Drupal 8?
Add new comment

Restricted HTML

Search

Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.

 

Contact Us

All the content is FREE but I still need your help

 

Become a patreon