I use Drupal 8 and I want to disable caching for a specific content type in full view mode.

I've found how to disable caching for all view modes, but I want to disable it for only a specific content type.

How can I do that?

2 Answers 2


You need to set max-age 0 in e.g. hook_node_view().

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

That said, I recommend you find a way to make caching work. There are concepts like lazy_builder that allow you cache most of a node and only dynamically build a small part in it.

Also note that this does not disable the page cache for anonymous users. If you need that too, which I would recommend even more to try and avoid, then you also need:


Update, answer is getting too long to post in a comment:

Ah, flag. How do you hide the flag for the author? Are you using the latest D8 and flag version?

If you look at flag_entity_view(), you can see that flag is already doing exactly that. It registers a so called lazy builder and enables a thing called placeholdering. It does that to show a flag/unflag link depending on the user. You probably just set #access on that element right now.

Flag is supposed to have access hooks but I think they don't work right now. See https://www.drupal.org/node/2584299. Follow that issue, once that is resolved, you can just implement that.

But for now, you can just switch out the lazy builder callback there with your own for now that does your additional check and then does what the existing one does. And it will just work.

  • our issue(I work with @victorcpereira) is regarding extra info being cached in undesired contexts. e.g. We have a Flag module link to "Follow" on a node page, allowing users to follow the node author. The problem is that when the author first see the node, it's get cached without the link(he shouldn't follow himself) and when another user, that should see the link, goes there, the cached version is presented. I'm thinking about creating separate blocks for each piece, because we know how to change cache for blocks, but I don't see how to remove cache for one piece inside main block.
    – pedrorocha
    Oct 8, 2015 at 22:22
  • 2
    Updated my answer.
    – Berdir
    Oct 9, 2015 at 6:17
  • Thanks for the update! Really useful info. Unfortunately for us, we started using Flag 8.x some weeks ago and they changed their API and data structure, so we decided to wait for a more stable version to make a final migration, but from what you said, maybe it's worth invest in migrating now to have this cache issue fixed. Huge thanks @berdir!
    – pedrorocha
    Oct 10, 2015 at 9:57
  • This answer is a good option and allows for complex conditions in code. Sep 29, 2021 at 15:31

You can also use the option

no_cache: TRUE

in your route declaration as described here: https://www.drupal.org/node/2463533

  • Nice addition. Cool to be able to do it on a route basis.
    – Christian
    Feb 22, 2021 at 3:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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