In drupal 7, what does make a menu link a stale item? I'm creating menu links with this kind of code:

$item = array(
  'link_path' =&gt; drupal_get_normal_path('link-path'),
  'link_title' =&gt; 'link-title',
  'menu_name' =&gt; 'menu-name',
  'weight' =&gt; 0,
  'expanded' =&gt; 1,
);

// Look the table first if the data does exist
$menuId = db_query("SELECT mlid from {menu_links} WHERE link_title=:link_title AND link_path=:link_path", array(':link_title' =&gt;  
$link['link_title'], ':link_path' =&gt; $link['link_path']))-&gt;fetchField();
if ($menuId === FALSE) {  
    $menuId = menu_link_save($item);
    echo "New entry " . $menuId . PHP_EOL;
} else {
    echo "exists " . $menuId . PHP_EOL;
}


Later on, if I call any of these functions:


menu_rebuild()
drupal_flush_all_caches()
Or if I clear the cache from the web ui.


All my menu links are deleted. According to the documentation, these functions "remove stale items from {menu_links}". And I am unable to find out what is missing in the parameters of menu_link_save or if I need an extra step. If I stop the script before flushing, the content table menu_link looks OK.

I'd like some explanations about the workflow of menu_rebuild that causes some objects to be deleted (i.e. is there a flag or something, etc.).
Is it related to Stale menu link static caches? https://www.drupal.org/node/2425261