File added programaticaly are not deleted when node is deleted I'm creating a node programmatically with one image field. Nodes are Created without any problem.

But if I delete the node the file is marked as "used in 0 place" but never deleted (I run the cron).

If I edit the node and delete the file of the image field, the file is still marked as "used in 1 place".



I want the file be deleted instantaneously like if I create the node using the node form.

/**
 * Create a new book from google data
 *
 * @param array $booksData
 * @return \Drupal\Core\Entity\EntityInterface\static
 * @throws Exception
 */
function book_market_create_book_node(Google_Service_Books_Volumes $booksData) {

  // Google_Service_Books_Volumes object automatically iterate, this will get data from the first book
  $bookData = $booksData[0];


  if (empty($bookData['modelData']['volumeInfo']['title'])) {
    throw new Exception('book data are incomplete');
  }

  $bookNode = Node::create([
    'type' =&gt; 'book',
    'moderation_state' =&gt; 'published',
    'title' =&gt; $bookData['modelData']['volumeInfo']['title'],
    'body' =&gt; $bookData['modelData']['volumeInfo']['description']
  ]);


  $imageLink = $bookData['modelData']['volumeInfo']['imageLinks']['smallThumbnail'];

  if (isset($imageLink)) {
    $data = file_get_contents($imageLink);

    $transliterator = new Drupal\Core\Transliteration\PHPTransliteration(NULL, \Drupal::moduleHandler());
    $fileName = $bookData['modelData']['volumeInfo']['title'];
    $fileName = str_replace(' ', '_', $fileName);
    $fileName = $transliterator-&gt;transliterate($fileName, 'fr', '_') . '.jpeg';

    $field_cover_settings = $bookNode-&gt;field_cover-&gt;getDataDefinition()
      -&gt;getSettings();
    $destination = $field_cover_settings['uri_scheme'] . '://' . $field_cover_settings['file_directory'] . '/' . $fileName;
    $destination = \Drupal::token()-&gt;replace($destination);



    $file = file_save_data($data, $destination, FILE_EXISTS_RENAME);

    $bookNode-&gt;set('field_cover', [
      'target_id' =&gt; $file-&gt;id(),
    ]);
  }

  $bookNode-&gt;enforceIsNew();
  $bookNode-&gt;save();

  return $bookNode;
}
I think that file will be delete when you run cron. File can't delete because temporary file have time life. You can see code in file_cron.
function file_cron() {
  $age = \Drupal::config('system.file')-&gt;get('temporary_maximum_age');
  $file_storage = \Drupal::entityManager()-&gt;getStorage('file');

  // Only delete temporary files if older than $age. Note that automatic cleanup
  // is disabled if $age set to 0.
  if ($age) {
    $fids = Drupal::entityQuery('file')
      -&gt;condition('status', FILE_STATUS_PERMANENT, '&lt;&gt;')
      -&gt;condition('changed', REQUEST_TIME - $age, '&lt;')
      -&gt;range(0, 100)
      -&gt;execute();
    $files = $file_storage-&gt;loadMultiple($fids);
    foreach ($files as $file) {
      $references = \Drupal::service('file.usage')-&gt;listUsage($file);
      if (empty($references)) {
        if (file_exists($file-&gt;getFileUri())) {
          $file-&gt;delete();
        }
        else {
          \Drupal::logger('file system')-&gt;error('Could not delete temporary file &quot;%path&quot; during garbage collection', array('%path' =&gt; $file-&gt;getFileUri()));
        }
      }
      else {
        \Drupal::logger('file system')-&gt;info('Did not delete temporary file &quot;%path&quot; during garbage collection because it is in use by the following modules: %modules.', array('%path' =&gt; $file-&gt;getFileUri(), '%modules' =&gt; implode(', ', array_keys($references))));
      }
    }
  }
}

You can see line if($age) and line -&gt;condition('changed', REQUEST_TIME - $age, '&lt;'). It's logic when run cron delete file.
And you can go admin/config/media/file-system to see time life of temporary file. Default are 6 hours.