I have activated the cache on my Drupal 8 site, and of course all pages are cached as expected.

However, I'd like to have a specific page bypass that cache.

In my controller I built the page as follows:

public function myPage() {
  return [
    '#markup' => time(),

How can I tell Drupal not to cache my page?


For Drupal 7:

Drupal has the function drupal_page_is_cacheable() which can be used to set a page to uncacheable.

Here is the documentation: https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/drupal_page_is_cacheable/7

For Drupal 8:

// Deny any page caching on the current request.    

Then the code is:

public function myPage() {
  return [
    '#markup' => time(),

As usual, clean your cache once done.


Disable cache for a specific page

Disable cache for a custom page from route declaration. If you want to disable cache for a custom controller (Custom module), You have no_cache option (YOUR_MODULE.routing.yml). Example : File : mymodule.routing.yml

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

Added 'no_cache' route option to mark a route's responses as uncacheable

  • Prefer this answer as you are setting route specific access as well in this same location, plus it's cleaner.
    – Ron
    May 20 '20 at 17:08

In Drupal 8, you can mention cache as max-age till you want your page ouput to be cached. For removing cache of a particular page(Controller page), write 'max-age' => 0,.

public function myPage() {
  return [
   '#markup' => time(),
   '#cache' => ['max-age' => 0,],    //Set cache for 0 seconds.

If you need to do something with a contrib module you can use a RouteSubscriber to fix the cache issue. you need a service ... or just run drupal gr if have drupal console.

 namespace Drupal\mymodule\Routing;

 use Drupal\Core\Routing\RouteSubscriberBase;
 use Symfony\Component\Routing\RouteCollection;

 class RouteSubscriber extends RouteSubscriberBase {

 * {@inheritdoc}
protected function alterRoutes(RouteCollection $collection) {
  // Find the route you need ...
  if ($route = $collection->get('some.contrib.route')) {
    $options = $route->getOptions();
    $options['no_cache'] = TRUE;

more info see https://www.drupal.org/docs/8/api/routing-system/altering-existing-routes-and-adding-new-routes-based-on-dynamic-ones


You can use this module to avoid drupal cache and also server side cache like varnish https://www.drupal.org/project/ape

Its works for me in Varnish cache on acquia server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.