Uploaded file management

Body

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() and marked as temporary, but will only become permanent if the form is submitted (and validated).

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 top 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:

Knowledge Category