Views are disappearing randomly; always needing to flush caches I am writing a module that has 2 views in code. At first, I pasted the code that was exported from views directly into hook_views_default_views() like so... 

function MY_MODULE_views_default_views() {
  $views = array();

  // Copy and pasted code from views export here...

  $views[$view-&gt;name] = $view;
  return $views;
}


After installing the module, the only way to get the views to show up would be to run cron and then flush the caches. I tried just running cron, or just flushing caches, and it would only work if I ran them both in that order. Once the views showed up, I could go along with things fine for a little while, then randomly the view would disappear again. I could not figure out a pattern to why they would disappear. Flushing caches would mostly fix it. Sometimes I had to flush caches two or three times or run cron and flush caches to get the views to appear again.

I searched around for some help and was able to find this post... A whole view disappears. This seems to be a similar problem. The function is too long and each view needs to be split into separate files. Okay, I did that and now my code looks like this...

// Files located in "my_module/views":
my_module.view_1.view
my_module.view_2.view

/**
 * Implementation of hook_views_default_views().
 */
function MY_MODULE_views_default_views() {
  $views = array();
  $files = file_scan_directory(drupal_get_path('module', 'MY_MODULE'). '/views', '/.*\.view$/');
  foreach ($files as $filepath =&gt; $file) {
    require $filepath;
    if (isset($view)) {
      $views[$view-&gt;name] = $view;
    }
  }
  return $views;
}


This is a nice solution as it makes adding new views very easy and clean. However, I am having the exact same experience as before. After installing the module, it still takes a cron run and a cache flush for the views to show, which they eventually do, but then they still disappear randomly. The only way to get them back is to flushes the caches (sometimes several times), or run cron and flush caches.

I'm feeling pretty stuck here. If anyone would be so kind as to offer some guidance, that would be most appreciated. Thank you.
Thank you so much to kladria on IRC #drupal-support for helping me through this one! 

After using a debugger to step through the code, I saw that my implementation of hook_views_default_views() was not being called. The problem I was having was that the function was in the wrong file. Originally, I had the function "MY_MODULE_views_default_views()" in the file "My_MODULE.views.inc", and it is supposed to go in "My_MODULE.views_default.inc". That "_default" made all the difference for it to be auto-loaded correctly.

So far, there has been no more needing to flush caches, and the views haven't disappeared. I'm pretty sure this did the trick.