Uploaded file management

Last updated on
14 August 2025

This documentation needs review. See "Help improve this page" in the sidebar.

Creation

When a file is uploaded using the Drupal API, it will become a "managed file" and Drupal will keep track of it (through the File API) in the database.

An uploaded file is placed in the a wrapper scheme and directory (by default: public://). It automatically creates a File entity with permanent status, and its usage counter is set to 1. This file will be display on the admin list of files (Content > Files, path: /admin/content/files) with at least one use.

Files are uploaded as soon as they are added to a field. They are analyzed by hook_file_validate() (deprecated in Drupal 10.2.0, see file_validate and related functions are deprecated and replaced with file.validator service and Constraint plugins) and marked as temporary, but will only become permanent if the form is submitted (and validated).

A more detailed description of the process

Let's say a node has a field of File type. After a user select a file for uploading the following actions occurs: 

1. The file uploaded in the temporary directory (the path to the directory is defined in the settings.php like  $settings['file_temp_path'] = '/tmp';). The file name in the temporary folder may not match the original file name, for example, it may be like "phpQTJJVm".

2. Once the file has been completely uploaded, it is placed in the a wrapper scheme and directory (by default: public://). It automatically creates a File entity with "Temporary" status. This file will be display on the admin list of files (Content > Files, path: /admin/content/files).

3. After the user saves the form with the file field, the file status changes to "Permanent" and its usage counter is set to 1. This file will be display on the admin list of files with at least one use.

Cleaning

Drupal natively incorporates a (physical) file deletion system. Here are the rules used by Drupal :

  • Any file in temporary status and which is no longer attached to an entity (usage at 0 or entry absent in the file_usage table) is considered deletable.
  • Any file in temporary status, which has not been altered (modified) since temporary_maximum_age (see system.file configuration) will be deleted the next time the Drupal cron runs (see file_cron() method). This property is set at "6 hours" by default, if set to "0" deletion is disabled. It is possible to modify it in Back Office in Configuration > Media > File system (/admin/config/media/file-system).

An alternate way to force the deletion of these files:

In file.settings set make_unused_managed_files_temporary property to true (default is 0) to activate the automatic deletion system for permanent files with a usage of 0.

This operation is only possible by modifying the configuration file, there is no Back Office interface to modify this property.

The activation of this property is not retroactive, already created files in permanent status with 0 usage will therefore not be deleted despite the activation of this property.

These modules could help to manage files:

Help improve this page

Page status: Needs review

You can: