diff --git a/taxonomy_menu.database.inc b/taxonomy_menu.database.inc
old mode 100644
new mode 100755
index 2f127e3..4843a11
--- a/taxonomy_menu.database.inc
+++ b/taxonomy_menu.database.inc
@@ -52,6 +52,31 @@ function _taxonomy_menu_get_menu_items($vid) {
   return $menu_items;
 }
 
+/**
+ * Get actual and orphaned menu relations for a vocab.
+ *
+ * @param $vid
+ *   vocabulary's id
+ * @return
+ *   array(tid => mlid)
+ */
+function _taxonomy_menu_get_menu_items_for_remove($vid) {
+  $voc = taxonomy_vocabulary_load($vid);
+  $menu_name = variable_get('taxonomy_menu_vocab_menu_' . $voc->machine_name, FALSE);
+
+  if (!empty($menu_name)) {
+    $result = db_query("SELECT ml.mlid FROM menu_links AS ml
+      LEFT JOIN taxonomy_menu AS tm ON ml.mlid = tm.mlid AND tm.vid = :vid
+      WHERE ml.menu_name = :menu_name AND ml.router_path = 'taxonomy/term/%'",
+      array(':vid' => $vid, ':menu_name' => $menu_name)
+    );
+    $menu_items = array();
+    $menu_items = $result->fetchCol();
+    return $menu_items;
+  }
+  return FALSE;
+}
+
  /**
   * Delete all links associated with this vocab from both the taxonomy_menu
   * table and the menu_link table.
@@ -60,9 +85,9 @@ function _taxonomy_menu_get_menu_items($vid) {
   *   vocabulary's id
   */
 function _taxonomy_menu_delete_all($vid) {
-  $menu_terms = _taxonomy_menu_get_menu_items($vid);
+  $menu_terms = _taxonomy_menu_get_menu_items_for_remove($vid);
   if (!empty($menu_terms)) {
-    foreach ($menu_terms as $tid => $mlid) {
+    foreach ($menu_terms as $mlid) {
       db_delete('menu_links')
         ->condition('mlid', $mlid)
         ->execute();
@@ -92,7 +117,6 @@ function _taxonomy_menu_get_node_terms($node) {
 
       foreach ($tid_field as $tid_field_languages) {
         // Loop through all the tids
-
         foreach ($tid_field_languages as $tid) {
           $tids[] = $tid['tid'];
         }
