file_cron()
Implements hook_cron().
File
- core/modules/file/file.module, line 646
- Defines a "managed_file" Form API field and a "file" field for Field module.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | function file_cron() { $age = \Drupal::config( 'system.file' )->get( 'temporary_maximum_age' ); $file_storage = \Drupal::entityManager()->getStorage( 'file' ); // Only delete temporary files if older than $age. Note that automatic cleanup // is disabled if $age set to 0. if ( $age ) { $fids = Drupal::entityQuery( 'file' ) ->condition( 'status' , FILE_STATUS_PERMANENT, '<>' ) ->condition( 'changed' , REQUEST_TIME - $age , '<' ) ->range(0, 100) ->execute(); $files = $file_storage ->loadMultiple( $fids ); foreach ( $files as $file ) { $references = \Drupal::service( 'file.usage' )->listUsage( $file ); if ( empty ( $references )) { if ( file_exists ( $file ->getFileUri())) { $file -> delete (); } else { \Drupal::logger( 'file system' )->error( 'Could not delete temporary file "%path" during garbage collection' , array ( '%path' => $file ->getFileUri())); } } else { \Drupal::logger( 'file system' )->info( 'Did not delete temporary file "%path" during garbage collection because it is in use by the following modules: %modules.' , array ( '%path' => $file ->getFileUri(), '%modules' => implode( ', ' , array_keys ( $references )))); } } } } |
Please login to continue.