How to disable cache for a custom page with programmetically

If you want to disable cache for a custom controller (Custom module), You have no_cache option (module_name.routing.yml).

File : example_module.routing.yml

  path: '/mymodule/mypage'
    _controller: '\Drupal\example_module\Controller\Pages::index'
    _title: 'No cache page'
    _access: 'TRUE'
    no_cache: 'TRUE'

Disable cache for a specific content type:

function example_module_node_view(array &$build, NodeInterface $node, $display, $view_mode) {
  if ($node->getType() == 'content_type_name' && $view_mode == 'full') {
    $build['#cache']['max-age'] = 0;

Disable cache from a controller.

Note : This will work in no cache on page level (not working for anonymous users for example).

Set cache max age on rederer array.

$output = [];
$output['#cache']['max-age'] = 0;


Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.