diff --git a/src/Entity/Flag.php b/src/Entity/Flag.php
index 166a0ef..6b01226 100644
--- a/src/Entity/Flag.php
+++ b/src/Entity/Flag.php
@@ -504,6 +504,28 @@ class Flag extends ConfigEntityBundleBase implements FlagInterface {
     }
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function postSave(EntityStorageInterface $storage, $update = TRUE) {
+    parent::postSave($storage, $update);
+    if (\Drupal::moduleHandler()->moduleExists('views')) {
+      // Rebuild views data to invalidate flag relationships.
+      \Drupal::service('views.views_data')->clear();
+    }
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function postDelete(EntityStorageInterface $storage, array $entities) {
+    parent::postDelete($storage, $entities);
+    if (\Drupal::moduleHandler()->moduleExists('views')) {
+      // Rebuild views data to invalidate flag relationships.
+      \Drupal::service('views.views_data')->clear();
+    }
+  }
+
   /**
    * Sorts the flag entities, putting disabled flags at the bottom.
    *
diff --git a/tests/src/FunctionalJavascript/FlagContextualLinksTest.php b/tests/src/FunctionalJavascript/FlagContextualLinksTest.php
index 301debb..c8d8fb0 100755
--- a/tests/src/FunctionalJavascript/FlagContextualLinksTest.php
+++ b/tests/src/FunctionalJavascript/FlagContextualLinksTest.php
@@ -157,6 +157,7 @@ public function setUp() {
     // Grant the flag permissions to the authenticated role.
     $this->grantFlagPermissions($this->flag);
 
+    $this->container->get('views.views_data')->clear();
   }
 
   /**
