Disable caching on a custom plugin block I read a lot of posts on how to disable caching on a custom block but none seemed to give the perfect solution.


$build['#cache'] = ['max-age' =&gt; 0] or public function getCacheMaxAge() { return 0; }: Do not seem to work, or at least for authenticated users
\Drupal::service('page_cache_kill_switch')-&gt;trigger(): Kill the complete page cache, not only the block ones
Lazy builders: Seems to work only for anonymous users


In my case, I have a block which renders a random node and it's clearly an issue while I'm not able to refresh this cache.

So how to set up such a block? (Drupal 8.4.3)

Posts giving not working solutions:


How to prevent a block from being cached?
How do I correctly setup caching for my custom block showing content depending on the current node?




Edit: 

public function build() {
    [...]
    if ($prev_node) {
      $render_controller = \Drupal::entityTypeManager()
        -&gt;getViewBuilder($prev_node-&gt;getEntityTypeId());
      $build = $render_controller-&gt;view($prev_node, 'pagination');
      $build['#node']-&gt;field_custom = 'something';
    }
    return $build;
}

public function getCacheMaxAge() {
    return 0;
}


Thank's to @Clive, it seems my issue is not linked to the block cache itself, but to the entity I render in this block. If so, I have no ideas how on to resolve this issue. 

After many searches, D7 cache_clear_all('field:node:10', 'my_field') function seems to do what I want to do. What is its equivalent for D8 ?
I got the same issue that you, here is what it worked for me.

/**
 * Implements hook_preprocess_HOOK() for block.html.twig.
 */
function MY_MODULE_preprocess_block(&amp;$variables) {
  $blocks_to_cache = [
    'SOME_block',
  ];
  if (in_array($variables['base_plugin_id'], $blocks_to_cache)) {
    $variables['#cache']['max-age'] = 0;
  }

}


So, In my my_module.module I search if I'm displaying this block, then I clean the cache for that block. You can several block names.