At this url:


There is a setting:

Delete temporary files after X hours

Does this specifically refer to files in /tmp ? Or whatever I set as my temporary folder? Or will it possibly delete "unused" files?

I ask, because I use file_save_data to save files. And I'm worried that Drupal at some points decides to delete some of these files, as they won't be referenced anywhere except in my own custom table.


Files uploaded to managed files will be set as temporary. You need to set the file status as permanent, and add a usage, to prevent the file from being deleted.

Setting files as permanent can be done using File::setPermanent(). File usages can be added as follows:

$fid = 1; // The file ID
$file = \Drupal\file\Entity\File::load($fid);
$file_usage = \Drupal::service('file.usage');
$file_usage->add($file, 'mymodule', $entity_type, $entity_id);

This will add a 'usage' for the file. When deleted, you need to remove the file usage. Files that have been set to permanent, and have a file usage applied, will not be deleted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.