I am using VIEW module to create a page which loads data from a database table.

Seems this view page is caching because whenever I add a new record in this table via custom input form, it doesn't show on view page immediately after refresh. I can clearly see this new entry in the database table but not on view page.

Then I have to manually clear cache from the site > performance section and then I can see new entry on the view page.

Please note that database table is a custom table and I am using hook_views_data() to add custom table fields in the view page.

BTW I had disabled 'Disable views data caching' on '/admin/structure/views/settings/advanced' page but it has no effect on my view page.

Can anyone please advise what should I do to avoid CACHE?


2 Answers 2


You have to add a cache tag to the View, the name is arbitrary, for example custom-data-list.

You can do this in a view hook:

function mymodule_views_pre_view($view, $display_id, array &$args) {
  if ($view->id() == 'my_custom_data_view') {
    $view->element['#cache']['tags'][] = 'custom-data-list`;

or use the module Views Custom Cache Tags.

By this all cache entries that contain the View, for example blocks or pages and the view itself are tagged with this tag.

Now the only thing you have to do, whenever you change the data in the database (this is for all CRUD operations, that write to the database), invoke:

  • Thanks for this. I have added above line just after db_merge() query after that it sends me to the view page which is same (no change). Not sure if above code is suppose to be after EDIT db_merge() query or anywhere else? BTW I am using version 8.3.2 Thanks Jun 19, 2017 at 11:10
  • Just rephrasing my feedback to clarify.... Can you shed light how to add the cache tag 'custom-data-list' in Views Custom Cache Tags? I have added \Drupal\Core\Cache\Cache::invalidateTags(['custom-data-list']); just after db_merge() query in my EDIT submit form code. Thanks Jun 19, 2017 at 11:20
  • you have to invalided on all CRUD operations, see the edit
    – 4uk4
    Jun 19, 2017 at 11:34
  • I have already invalidated EDIT function but I believe first step is to define / declare 'custom-data-list'? how to add the cache tag 'custom-data-list' in Views Custom Cache Tags? Jun 19, 2017 at 12:59
  • I've never used the module, but looking at the source code you get an option "Custom tag list" in your view. Or use a hook instead of the module, see the code example in the answer.
    – 4uk4
    Jun 19, 2017 at 13:12

The only way I found to add a custom cache tag to a view is implementing a views cache plugin.

Adding the tag as @4k4 suggested doesn't works. I have tried using different views hooks and different ways without be able to get it working.

With $view->element['#cache']['tags'][] = 'custom-data-list' or $view->storage->addCacheTags(['custom-data-list']) the tag appears in X-Drupal-Cache-Tags header, but the view cache is not invalidated when I invalidated the tag.

Then investigating Views Custom Cache Tags module code I found a way to implement it in a custom class:

namespace Drupal\data_view\Plugin\views\cache;

use Drupal\views\Plugin\views\cache\Tag;

* Add custom cache tags for rows in custom tables.
* @ingroup views_cache_plugins
* @ViewsCache(
*   id = "table_tag",
*   title = @Translation("Tag for custom tables"),
*   help = @Translation("Tag based caching of data. Caches will persist until any table cache tag are invalidated.")
* )
class TableTag extends Tag {

  * {@inheritdoc}
  public function summaryTitle() {
    return $this->t('Table tag');

  * {@inheritdoc}
  public function getCacheTags() {
    // Here you can also create tags for single records in the form "your-table:id"
    return ['custom-data-list'];


Then in the view settings page, in the Other section, select Tags for custom tables in Caching.

In your code don't forget to invalidate the cache every time you change a record in your custom table.

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.