--- a/entityreference.module	Tue Mar 20 21:43:37 2012
+++ b/entityreference.module	Mon Apr 02 09:22:46 2012
@@ -378,6 +378,26 @@
     '#limit_validation_errors' => array(),
   );
 
+  // Option to remove field values when the entity they reference is deleted.
+  $cleanup_options = array(
+    0 => t('Leave invalid references around when entities are deleted.'),
+    1 => t('Delete references in this field when a referenced entity is deleted.'),
+    );
+  $form['dereference_on_delete'] = array(
+    '#type' => 'radios',
+    '#title' => t('Cleanup'),
+    '#title_display' => 'before',
+    '#description' => t('What to do when a referenced entity is deleted. Note that the second option may cause timeouts or memory issues when deleting large amounts of entities or references.'),
+    '#options' => $cleanup_options,
+    '#default_value' => isset($field['settings']['dereference_on_delete']) ? $field['settings']['dereference_on_delete'] : 0,
+    '#ajax' => array(
+      'callback' => 'entityreference_settings_ajax',
+      'wrapper' => $form['#id'],
+      'element' => $form['#array_parents'],
+    ),
+    '#limit_validation_errors' => array(),
+  );
+
   ctools_include('plugins');
   $handlers = ctools_get_plugins('entityreference', 'selection');
   uasort($handlers, 'ctools_plugin_sort');
@@ -989,4 +1009,47 @@
     'api' => 3,
     'path' => drupal_get_path('module', 'entityreference') . '/views',
   );
+}
+
+/*
+ * Implements hook_entity_delete().
+*/
+function entityreference_entity_delete($entity, $type) {
+  // Get id of entity.
+  list($entity_id, , ) = entity_extract_ids($type, $entity);
+  // Retrieve info for all entityreference fields.
+  $conditions = array('type' => 'entityreference');
+  $include_additional = array('include_inactive' => TRUE);
+  $fields = field_read_fields($conditions, $include_additional);
+  // Loop through the fields.
+  foreach ($fields as $field) {
+    // Determine the target type of the field.
+    $target_type = $field['settings']['target_type'];
+    $cleanup_enabled = (isset($field['settings']['dereference_on_delete']) && $field['settings']['dereference_on_delete']);
+    // Check if cleanup is enabled and the field references the same type as the referenced entity.
+    if ($cleanup_enabled && $target_type == $type) {
+      $query = new EntityFieldQuery();
+      // Select on the entity id in the target_id column.
+      $query->fieldCondition($field, 'target_id', $entity_id);
+      $references = $query->execute();
+      // Loop through the found entity types and ids.
+      foreach ($references as $referencing_type => $ids) {
+        // Load all referencing entities of this type.
+        $referencing_entities = entity_load($referencing_type, array_keys($ids));
+        foreach ($referencing_entities as $referencing_entity) {
+          // Get all the values in the field.
+          $items = $referencing_entity->{$field['field_name']}[$referencing_entity->language];
+          // Loop through them to delete the specific value of the field that references the deleted entity.
+          foreach($items as $delta => $value) {
+            if ($value['target_id'] == $entity_id) {
+              unset($referencing_entity->{$field['field_name']}[$referencing_entity->language][$delta]);
+            }
+          }
+          // Store the modified entity. This should take care of all modules wanting to know about
+          // the changes and of flushing the relevant caches.
+          entity_save($referencing_type, $referencing_entity);
+        }
+      }
+    }
+  }
 }
