Remove uploaded files before the 6 hours cron? I have a custom Content Entity in wich definition there is a 'file' field.
The file widget works fine.
The 'Browse' button allows to select a file and uploads it to the proper server directory and the 'file_managed' and 'file_usage' tables are updated accordingly.
If I delete the entity, 'file_usage' record is deleted and in 'file_managed' table, the record has its status field set to '0'.
What I understood is that cron job removes all files in the directory with status = '0' after 6 hours.
My question is: how to have the file removed just after the submission of the delete form?
file_cron() contains the code that delete managed files. To adapt it to delete those files with their status set to 0, this is what I would use.

$file_storage = \Drupal::entityTypeManager()-&gt;getStorage('file');
$fids = Drupal::entityQuery('file')-&gt;condition('status', 0)-&gt;execute();
$files = $file_storage-&gt;loadMultiple($fids);

foreach ($files as $file) {
  $references = \Drupal::service('file.usage')-&gt;listUsage($file);
  if (empty($references) &amp;&amp; file_exists($file-&gt;getFileUri())) {
    $file-&gt;delete();
  }
}


This code returns you all the files matching those conditions. You can also use the following code to get them in batches of 100 per time.

$file_storage = \Drupal::entityTypeManager()-&gt;getStorage('file');
$fids = Drupal::entityQuery('file')-&gt;condition('status', 0)-&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) &amp;&amp; file_exists($file-&gt;getFileUri())) {
    $file-&gt;delete();
  }
}


I didn't check, but it is probable you can also filter the returned file entities by the bundle using them.