Managed file: the file used in the field may not be referenced
The solution is to add a call to file_usage_add after the file is saved. This call adds a reference to the file in the database. The file_managed_file_validate function will spot the reference and won't trigger the error.

file_usage_add($file, 'foo', 'foo', $foo-&gt;id);


This isn't documented in the Form API under the managed_file example.

This issue is being discussed on the Drupal documentation issues: link