Drupal 8 reset cache for the page programmaticaly

  1. function custom_module_page_alter(&$page) {
  2.  
  3. if (\Drupal::service('path.current')->getPath() == '/the-page') {
  4. Drupal::entityManager()->getViewBuilder('node')->resetCache();
  5. }
  6. }

Another way:

  1. function custom_module_node_view(&$build, \Drupal\node\NodeInterface $node, $display, $view_mode) {
  2. if ($node->getType() == 'article' && $view_mode == 'full') {
  3. $build['#cache']['max-age'] = 0;
  4. }
  5. }
  6.  
  7. function custom_module_nodes_cache() {
  8. $module_handler = \Drupal::moduleHandler();
  9. $module_handler->invokeAll('cache_flush');
  10. foreach (Cache::getBins() as $service_id => $cache_backend) {
  11. $cache_backend->deleteAll();
  12. }
  13. PhpStorageFactory::get('twig')->deleteAll();
  14. }