The default setting given to delete temp files is after 6 hours minimum. I have users upload files and I want that if it has temp status, delete it after 15 secs. Tweaking the file module code is risky, if I get the exact location to change 6 hours to 15 sec that will do the job.

  • That also means you need to have a cron job running every 15 seconds. That job then triggers drush cron maybe. Can you maybe paste a link to the exact line of the file module on git.drupalcode.org you identified being responsible for the 6h minimum into your question? – leymannx Mar 22 at 6:29

The value stored in this config

\Drupal::config('system.file')->set('temporary_maximum_age', '15');

You can update that with the value you wants.

If you want to change the form as well which is recommended use hook_form_FORM_ID_alter to adjust the system file settings from system_file_system_settings

The current implementation looks like this

$intervals = [0, 21600, 43200, 86400, 604800, 2419200, 7776000];
$period = array_combine($intervals, array_map([$this->dateFormatter, 'formatInterval'], $intervals));
$period[0] = $this->t('Never');
$form['temporary_maximum_age'] = [
  '#type' => 'select',
  '#title' => $this->t('Delete temporary files after'),
  '#default_value' => $config->get('temporary_maximum_age'),
  '#options' => $period,
  '#description' => $this->t('Temporary files are not referenced, but are in the file system and therefore may show up in administrative lists. <strong>Warning:</strong> If enabled, temporary files will be permanently deleted and may not be recoverable.'),

You can alter the form and adjust it to what you want.

Keep in mind, to see this happen your corn must run at least every 15 sec.

  • Thank you @Ziftman, I did the same and changed 21600 to 15 in /core/modules/system/src/Form/FileSystemForm.php and uploaded a new temp file. However, the file was not deleted automatically after 15 seconds. Please tell me what did I miss. – Pankhuri Singh Mar 22 at 10:15
  • @PankhuriSingh you should not change the core file, you can override that from your own custom module, and in order to have the file deleted your corn should run at least every 15sec, that's why I told you it makes no sense to do that. in case you really want to do that every 15 sec then you need to do it without depending on corn. – Ziftman Mar 22 at 10:27

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.