Skip to content

Instantly share code, notes, and snippets.

@vyspiansky
Last active August 22, 2024 08:19
Show Gist options
  • Save vyspiansky/c35574984be0c002542a9c1bc2b34558 to your computer and use it in GitHub Desktop.
Save vyspiansky/c35574984be0c002542a9c1bc2b34558 to your computer and use it in GitHub Desktop.
Clear cache programmatically in Drupal 8+

Clear cache by specific tags

\Drupal::service('cache_tags.invalidator')->invalidateTags([$tag1, $tag2]);

Clear specific cache like render cache

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

Clear block render cache

\Drupal::cache('block')->invalidateAll();

Clear cache of an entity

\Drupal::entityTypeManager()->getStorage($entity_type_id)->resetCache([$id]);

// or
\Drupal\Core\Cache\Cache::invalidateTags($entity->getCacheTagsToInvalidate());

Clear render cache by cache ID

\Drupal::service('cache.render')->invalidate($cid);

Clear all cache

drupal_flush_all_caches();

Clear cache by SQL-query

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`;

Clear cache by Drush command

drush cr all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment