Showing posts with label delete files. Show all posts
Showing posts with label delete files. Show all posts

Monday 19 April 2021

How to remove or cleanup old aggregated css and js files?

It's very problematic question that how we remove old aggregated css and js files otherwise folder will be too large.

In drupal we have 2 functions drupal_clear_css_cache() and drupal_clear_js_cache() that are creating our aggregation files and store in css and js folder in files/css or files/js folder.

In drupal we have on special variable `drupal_stale_file_threshold` and the default value of this 30 days, it means after 30 days old aggregated file will automatically removed and we can change this 30 days value or less then 30 days like..

Code:

variable_get('drupal_stale_file_threshold', 2592000) //for 30 day. 

variable_set('drupal_stale_file_threshold', 172800) //for 2 days.  


Note: In drupal, we have advance aggregation module, there we have delete old aggregated files option.



Note: If have any suggestions or issue regarding 'How to remove or cleanup old aggregated css and js files?' then you can ask by comments. 

How to permanent delete unused files in Drupal 8?

Drupal 8, we can programmatically delete our unused files(assets) easily. we just have to write the code in custom module file in `hook_cron` function. After put the code and clear the cache whenever you hit the cron manually or automatically will run this code will execute.

Code:

/**

 * override hook_cron()

 */

function mymodule_cron()  // mymodule will replace with module name

{

  // get all files ids

  $fids = Drupal::entityQuery('file')->execute();

  $file_usage = Drupal::service('file.usage');

  // loop all fids and load files by fid

  foreach ($fids as $fid) {

    $file = Drupal\file\Entity\File::load($fid);

    $usage = $file_usage->listUsage($file);

    // check if file not used

    if (count($usage) == 0) {

      $file->delete();

    }

  }

}


Note: If have any suggestions or issue regarding 'How to permanent delete unused files in Drupal 8?' then you can ask by comments. 

How to resolve max execution time error in drupal ?

When you found error regarding 'max_execution_time' exceed, then you can follow steps for resolve this error: Steps:   You can put t...