diff --git a/flag.module b/flag.module
index 03550c6..2f9f958 100644
--- a/flag.module
+++ b/flag.module
@@ -275,13 +275,23 @@ function flag_user_account_removal(UserInterface $account) {
   // Remove flags by this user.
   $flaggings = $flag_service->getFlaggings(NULL, NULL, $account);
   foreach ($flaggings as $flagging_id => $flagging) {
-    $flag_service->unflag($flagging->getFlag(), $flagging->getFlaggable(), $account);
+    if ($flaggable = $flagging->getFlaggable()) {
+      $flag_service->unflag($flagging->getFlag(), $flaggable, $account);
+    }
+    else {
+      $flagging->delete();
+    }
   }
 
   // Remove flags that have been done to this user.
   $flaggings = $flag_service->getFlaggings(NULL, $account);
   foreach ($flaggings as $flagging_id => $flagging) {
-    $flag_service->unflag($flagging->getFlag(), $flagging->getFlaggable());
+    if ($flaggable = $flagging->getFlaggable()) {
+      $flag_service->unflag($flagging->getFlag(), $flaggable);
+    }
+    else {
+      $flagging->delete();
+    }
   }
 }
 
diff --git a/src/Tests/FlagSimpleTest.php b/src/Tests/FlagSimpleTest.php
index 1c97f0c..b82384a 100644
--- a/src/Tests/FlagSimpleTest.php
+++ b/src/Tests/FlagSimpleTest.php
@@ -254,36 +254,38 @@ class FlagSimpleTest extends WebTestBase {
   public function doUserDeletionTest() {
     $node = $this->drupalCreateNode(['type' => $this->nodeType]);
     $node_id = $node->id();
+    $node2 = $this->drupalCreateNode(['type' => $this->nodeType]);
 
     // Create and login a new user.
-    $user_1 = $this->drupalCreateUser();
+    $user_1 = $this->drupalCreateUser(['delete any article content']);
     $this->drupalLogin($user_1);
 
+    // Flag both nodes.
     $this->drupalGet('node/' . $node_id);
     $this->clickLink('Flag this item');
     $this->assertResponse(200);
     $this->assertLink('Unflag this item');
+    $this->drupalGet('node/' . $node2->id());
+    $this->clickLink('Flag this item');
+    $this->assertResponse(200);
 
-    $count_flags_before = \Drupal::entityQuery('flagging')
-      ->condition('uid', $user_1->id())
-      ->condition('flag_id', $this->id)
-      ->condition('entity_type', $node->getEntityTypeId())
-      ->condition('entity_id', $node_id)
-      ->count()
-      ->execute();
+    // Ensure both of them are flagged.
+    $count_flags_before = $this->countFlagging($user_1, $node);
+    $this->assertTrue(2, $count_flags_before);
+
+    // Delete one node.
+    $this->drupalPostForm('node/' . $node2->id() . '/delete', [], t('Delete'));
+    $this->assertResponse(200);
 
+    // Ensure there is still one flagged.
+    $count_flags_before = $this->countFlagging($user_1, $node);
     $this->assertTrue(1, $count_flags_before);
 
+    // Delete the user.
     $user_1->delete();
 
-    $count_flags_after = \Drupal::entityQuery('flagging')
-      ->condition('uid', $user_1->id())
-      ->condition('flag_id', $this->id)
-      ->condition('entity_type', $node->getEntityTypeId())
-      ->condition('entity_id', $node_id)
-      ->count()
-      ->execute();
-
+    // Ensure that all the flags are deleted.
+    $count_flags_after = $this->countFlagging($user_1, $node);
     $this->assertEqual(0, $count_flags_after);
   }
 
@@ -361,4 +363,16 @@ class FlagSimpleTest extends WebTestBase {
 
     $this->assertEqual(0, $count_flags_before);
   }
+
+  protected function countFlagging($user, $node) {
+    // Ensure there is still one flagged.
+    return $count_flags = \Drupal::entityQuery('flagging')
+      ->condition('uid', $user->id())
+      ->condition('flag_id', $this->id)
+      ->condition('entity_type', $node->getEntityTypeId())
+      ->condition('entity_id', $node->id())
+      ->count()
+      ->execute();
+  }
+
 }
