I have a custom block that reads information from the database. As the user will be filling out a form, refreshing the page and viewing the block it cannot be cached. How do I fix this?

I have tried:

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

And this did not fix it.

I also tried copying the code from the custom block to the top of the form, so it runs as part of the page, here it doesn't cache. Unfortunately this isn't a solution as I need the block on another page also.

1 Answer 1


Drupal caches the already rendered block and doesn't check for changes in code, so after changing #cache in the block's render array as shown, you'd still need to use drush or the UI option to force-clear the render cache after changing this.

As an alternative, try deleting the block instance and placing it again.

Since you don't indicate that the form is making changes to the database, it's not clear if there is any cache tag to associate - is it data input or something more like a search form?

  • The block pretty much gets ` $user_expiry = strtotime($user->get('field_expiry_date')->value);` and displays it as days remaining. Therefore it needs to change every day. Commented Jun 25, 2016 at 21:59
  • So, it's a per-user value? You could possibly use the user cache context and a longer max-age, but it's probably not worth it.
    – pwolanin
    Commented Jun 25, 2016 at 23:51

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.