diff --git a/core/modules/views/src/Plugin/views/cache/CachePluginBase.php b/core/modules/views/src/Plugin/views/cache/CachePluginBase.php
index 14ac7994fd..adffb6e9e7 100644
--- a/core/modules/views/src/Plugin/views/cache/CachePluginBase.php
+++ b/core/modules/views/src/Plugin/views/cache/CachePluginBase.php
@@ -315,6 +315,9 @@ public function getRowCacheTags(ResultRow $row) {
 
     if (!empty($row->_relationship_entities)) {
       foreach ($row->_relationship_entities as $entity) {
+        if (empty($entity)) {
+          continue;
+        }
         $tags = Cache::mergeTags($tags, $entity->getCacheTags());
       }
     }
diff --git a/core/modules/views/src/Plugin/views/query/Sql.php b/core/modules/views/src/Plugin/views/query/Sql.php
index 7503e1d8b1..a78feaac7e 100644
--- a/core/modules/views/src/Plugin/views/query/Sql.php
+++ b/core/modules/views/src/Plugin/views/query/Sql.php
@@ -1674,6 +1674,9 @@ public function getCacheTags() {
     // Add cache tags for each row, if there is an entity associated with it.
     if (!$this->hasAggregate) {
       foreach ($this->getAllEntities() as $entity) {
+        if (empty($entity)) {
+          continue;
+        }
         $tags = Cache::mergeTags($entity->getCacheTags(), $tags);
       }
     }
@@ -1687,6 +1690,9 @@ public function getCacheTags() {
   public function getCacheMaxAge() {
     $max_age = parent::getCacheMaxAge();
     foreach ($this->getAllEntities() as $entity) {
+      if (empty($entity)) {
+        continue;
+      }
       $max_age = Cache::mergeMaxAges($max_age, $entity->getCacheMaxAge());
     }
 
