How to clear cache programmatically in Drupal 8?
Asked Answered
N

5

13

I am working on a Drupal 8 site. This site was working. I recently moved to another machine.

It is showing errors like:

Recoverable fatal error: Argument 1 passed to 

Drupal\Component\DependencyInjection\Container::__construct() must be of

 the type array, boolean given, called in [DRUPAL-

PATH]/core/lib/Drupal/Core/DrupalKernel.php on line 883 and defined in 

[DRUPAL-PATH]/core/lib/Drupal/Component/DependencyInjection/Container.php

 on line 119 #0 [DRUPAL-PATH]/core/includes/bootstrap.inc(550): 

_drupal_error_handler_real(4096, 'Argument 1 pass...', 

'/Applications/M...', 119, Array)

I need to clear cache programmatically.

Neptunium answered 25/4, 2017 at 10:31 Comment(0)
G
4

By SQL

TRUNCATE `cache_bootstrap`;
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_rest`;
TRUNCATE `cachetags`;
TRUNCATE `cache_toolbar`;

By Drush

drush cr all
Geosphere answered 27/4, 2017 at 4:44 Comment(1)
As mentionned by sreekanth kuriyala, theres a function for it : drupal_flush_all_caches()Gyrus
R
32

Try below.

cache_clear_all() // For Drupal-7

drupal_flush_all_caches() // For Drupal-8
Renie answered 27/4, 2017 at 5:0 Comment(1)
This should be the chosen answer since the OP said "programmatically".Clot
E
26

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 all the cache then try:

drupal_flush_all_caches()

The following services implement the CacheBackendInterface and has invalidateAll() method which marks all the cache items as invalid:

cache.bootstrap
cache.config
cache.data
cache.default
cache.discovery
cache.entity
cache.menu
cache.render
cache.static
Etz answered 23/6, 2019 at 16:17 Comment(1)
If you want to be even more granular, then figure out the cache ID and \Drupal::service('cache.render')->invalidate($cid).Perales
G
4

By SQL

TRUNCATE `cache_bootstrap`;
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_rest`;
TRUNCATE `cachetags`;
TRUNCATE `cache_toolbar`;

By Drush

drush cr all
Geosphere answered 27/4, 2017 at 4:44 Comment(1)
As mentionned by sreekanth kuriyala, theres a function for it : drupal_flush_all_caches()Gyrus
G
4

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

drupal_flush_all_caches();

there are 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 a specific cache like route-cache then you can run the following code:

\Drupal::service("router.builder")->rebuild();
Ginglymus answered 23/3, 2021 at 5:30 Comment(0)
P
2

Just in case someone is looking for a way to clear the cache of all views:

views_invalidate_cache();

is your friend!

Perch answered 26/7, 2022 at 17:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.