Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear cache programmatically in Drupal 8?

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.

like image 868
Pupil Avatar asked Apr 25 '17 10:04

Pupil


People also ask

How do I clear cache in Drupal?

To clear all caches, use the cache-rebuild command: drush cache-rebuild . This will empty all caches and rebuild the data required for Drupal to execute a page request. Alternatively, use the aliased commands drush cr or drush rebuild .

Which tab is clear cache Drupal?

Primary tabs To clear cache in Drupal you have to navigate Configuration->Performance->Clear All Caches. But with this module, you can get a button on Admin Menu to clear cache and Save time.

How do I clear my menu cache?

Notes: In most computer-based web browsers, you can open menus used to clear cache, cookies, and history, by pressing Ctrl-Shift-Delete (Windows) or Command-Shift-Delete (Mac).


Video Answer


3 Answers

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
like image 142
Malik Naik Avatar answered Oct 18 '22 02:10

Malik Naik


Try below.

cache_clear_all() // For Drupal-7

drupal_flush_all_caches() // For Drupal-8
like image 30
sreekanth kuriyala Avatar answered Oct 18 '22 03:10

sreekanth kuriyala


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
like image 4
vijay Avatar answered Oct 18 '22 03:10

vijay