diff --git a/entityreference.install b/entityreference.install
index 4e248ef..1ca34c9 100644
--- a/entityreference.install
+++ b/entityreference.install
@@ -161,4 +161,109 @@ function entityreference_update_7002() {
       'not null' => TRUE,
     ));
   }
-}
\ No newline at end of file
+}
+
+/**
+ * Remove all entity node references that reference nodes that have been
+ * deleted and update the node that contains that reference.
+ */
+function entityreference_update_7003(&$sandbox) {
+
+  if (!isset($sandbox['total'])) {
+    $result = db_query('SELECT * FROM {node}');
+    $sandbox['total'] = $result->rowCount();
+    $sandbox['progress'] = 0;
+    $sandbox['messages'] = array();
+    // Image resize filter can cause errors. Its variable defaults to '10'.
+    $image_resize = variable_get('image_resize_filter_threshold');
+    if (isset($image_resize)) {
+      // Store the variable value if there is one.
+      $sandbox['resize_filter'] = $image_resize;
+    }
+    // Set the threshold high. We have to assign a value here so it's not
+    // overridden later.
+    variable_set('image_resize_filter_threshold', 100);
+  }
+
+  // The number of nodes we will process per batch.
+  $num_nodes = 100;
+
+  // Get the nodes we will be processing.
+  $result = db_select('node', 'n')
+    ->fields('n', array('nid'))
+    ->range($sandbox['progress'], $num_nodes)
+    ->execute();
+
+  // Loop over each result.
+  while ($row = $result->fetchAssoc()) {
+    // Store nids we have already checked.
+    static $nid_map;
+    $nid_map = !empty($nid_map) ? $nid_map : array();
+    // Load the node.
+    $node = node_load($row['nid']);
+    // Update progress when node is loaded
+    $sandbox['progress']++;
+    // Get the enitity bundle.
+    list(, , $bundle) = entity_extract_ids('node', $node);
+    // Get information about the field.
+    $instances = field_info_instances('node', $bundle);
+    foreach ($instances as $field_name => $instance) {
+      $field = field_info_field($field_name);
+      // We only continue if the target is a node and it is an enitityreference.
+      if ($field['type'] == 'entityreference' && $field['settings']['target_type'] == 'node') {
+        // Make sure the field isn't empty and an id exists.
+        if (!empty($node->{$field_name}) && $id = $node->{$field_name}[LANGUAGE_NONE][0]['target_id']) {
+          // Make sure the node does not exist.
+          if (!node_load($id)) {
+            // Start a new EFQ
+            $efq = new EntityFieldQuery();
+            // Find entities that are referenced by this node.
+            $efq->entityCondition('entity_type', 'node')
+              ->fieldCondition($field_name, 'target_id', $id, '=');
+            // Execute query, results with have 'node' key
+            $efq_result = $efq->execute();
+            if (isset($efq_result['node'])) {
+              // Tried loading nodes like this but getting errors with drush updb.
+              $nids = array_keys($efq_result['node']);
+              $related_nodes = node_load_multiple($nids);
+
+              foreach ($related_nodes as $related_node) {
+                $edit_wrapper = entity_metadata_wrapper('node', $related_node);
+                $edit_wrapper->{$field_name} = NULL;
+                $edit_wrapper->save();
+                drupal_set_message(t('The node: @nid was updated. The entity reference field: @field, was referencing a node with the id: @id that does not exist.', array(
+                  '@nid' => $related_node->nid,
+                  '@field' => $field_name,
+                  '@id' => $id,
+                )));
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+
+  // Set a message on our progress.
+  drupal_set_message(t('Progress: @progress', array(
+    '@progress' => $sandbox['progress'] . '/' . $sandbox['total'],
+  )));
+
+  // Determines when we have processed all nodes.
+  $sandbox['#finished'] = ($sandbox ['progress'] >= $sandbox ['total']) ? TRUE : ($sandbox ['progress'] / $sandbox ['total']);
+  // We are finished with the full batch.
+  if ($sandbox['#finished'] == 1) {
+    // If we found a value on this variable set it back.
+    if (isset($sandbox['resize_filter'])) {
+      variable_set('image_resize_filter_threshold', $sandbox['resize_filter']);
+      drupal_set_message(t('Variable: "image_resize_filter_threshold", set back to ' . $sandbox['resize_filter']));
+    }
+    // Otherwise just delete the variable.
+    else {
+      drupal_set_message(t('The variable: "image_resize_filter_threshold" was deleted.'));
+      variable_del('image_resize_filter_threshold');
+    }
+    // Set a final message with the total number of nodes processed.
+    drupal_set_message(t('We processed @nodes nodes.', array('@nodes' => $sandbox['total'])));
+  }
+}
