Drupal 8, The file used in the Image field may not be referenced I am using managed file type to upload image in my custom form (Drupal-8). I am able to retrieve the given image while editing by using the below code

$form['image_field'] = array(
   '#type' =&gt; 'managed_file',
   '#title' =&gt; t('Image'),
   '#default_value' =&gt; array($fid),
); 


While i am trying to save once edit, i am getting the "The file used in the Image field may not be referenced." error. 



Please let me know to over come this error and let me what this error trying to convey.
Check \Drupal\file\Element\ManagedFile for answer, it appears that your file is being saved as permanent, but there is no entry in file_usage table:

if ($file = File::load($fid)) {
      if ($file-&gt;isPermanent()) {
        $references = static::fileUsage()-&gt;listUsage($file);
        if (empty($references)) {
          // We expect the field name placeholder value to be wrapped in t()
          // here, so it won't be escaped again as it's already marked safe.
          $form_state-&gt;setError($element, t('The file used in the @name field may not be referenced.', ['@name' =&gt; $element['#title']]));
        }
      }
    }


If you want to store your files permanently, you need to make sure they've got usage entries, to do so, you can tweak you form submit handler a little bit, here's some quick example:

// get file fid wherever it is
$values = $form_state-&gt;getValues();
$fid = $values['my_file_field'];

// call file usage service and load file
$file_usage = \Drupal::service('file.usage');
$file = File::load($fid);

// set file status permanent
if(!$file-&gt;isPermanent()){
  $file-&gt;setPermanent();
}

// check file usage , if it's empty, add new entry
$usage = $file_usage-&gt;listUsage($file);
if(empty($usage)){
  // let's assume it's image
  $file_usage-&gt;add($file,'mymodule','image',$fid);
}
$file-&gt;save();