6

When aggregating CSS/JavaScript files using performance page in Drupal 8, drush cc css-js doesn't work, and I've to clear/rebuild all cache.

How do I properly clear/rebuild only the JavaScript/CSS cache?

I want that the filenames change too, so that the browser resets its cache too.

2
  • 1
    You can disable aggregation in the settings.local.php file. Commented Jul 1, 2016 at 8:47
  • but i want to keep aggregation enable :) Commented Jul 2, 2016 at 9:52

3 Answers 3

4

NOTE: I am using the s3fs plugin in order to store assets in an AWS S3 bucket, so if you are not using that your results could differ

This is probably not a suggested method of forcing the system to clear the asset cache, but I was in the same boat and for some reason my file systems got mixed up during multiple edits between local, development, and the live site. For the live site I was only able to get the styles working if the files were not aggregated using the Performance page.

As far as I know, the system will not update the CSS or JS files unless a change has been detected. Aside from going in and making a change to every file I started digging into the database. I found that the file location for the CSS and JS assets were located in the "cache_default" table with a value of something like below:

s3fs:uri:public://css/css_0WFR6_Et4WEJOkLOOpdg4f7EmvMRhsPkSk7KcrPyct4.css

and

s3fs:uri:public://js/js_-YxTfPGZervj7hTx1tF6zyt9ua2DxHPROTbub3I_WRg.js

BACKUP YOUR DATABASE BEFORE GOING FURTHER! In case you mess anything up, CYA!

  1. I went through and deleted all entries that were a CSS or JS file. Be careful not to delete the directory reference entries that just have the JS and CSS folders.
  2. THEN, since I was using s3fs I went into the "s3fs_file" table and did the same thing.
  3. I went back into the Performance page, enabled the CSS and JS aggregation, and saved.
  4. Then cleared the site cache after that. This seemed to re-generate the CSS and JS assets, re-built the asset table data, and the site was back to normal!

I did have to dump the DB tables and import into my dev and local environments in order to mimic the new asset names, but this seemed to work like a charm.

SIDE NOTE: I am wondering if there would be some way for Drupal to add a style versioning system? Maybe that we could change the sites CSS versioning after major changes in order to have this done automatically? or maybe this is just something I will need to build my next module for!!

1
  • 1
    To add to this, as an alternative route when using the s3fs module, inside the drupal admin, if you traverse to "Configuration" -> "Media" -> "S3 File System Settings" -> "Actions", and then toggle "Refresh file metadata cache", that will look through all saved S3 files on the bucket, if it cannot find the css/js file, no metadata will be set for the file object, so on next cache clear it will rebuild the css/js files missing. Commented Oct 27, 2023 at 17:32
0

Drupal 8 Using Drush you can clear the generated files using

drush advagg-clear-all-files 

or to force-generating new files by incrementing a global counter use

drush advagg-force-new-aggregates 
-1

You can run drush ev command to clear css-js cache, check example below:

drush ev '\Drupal::service("asset.css.collection_optimizer")->deleteAll(); \Drupal::service("asset.js.collection_optimizer")->deleteAll(); _drupal_flush_css_js();' 

Above sample is taken from here.

4
  • This seem good, but i can't test it until monday, i'll accept your answer at this time :) Commented Jul 2, 2016 at 9:55
  • ok i tested right know. I forgot to say it but i want that the file name change too , otherwith the brother don't reset his cache either. This command reset cache, but keep the same file name :/ Commented Jul 4, 2016 at 13:56
  • sorry i fact it's not reset aggregate file neither . it seam to do the same as drush cc css-js Commented Jul 7, 2016 at 10:12
  • This no longer seems valid. I used only de css part: drush ev '\Drupal::service("asset.css.collection_optimizer")->deleteAll(); _drupal_flush_css_js();' It did not clear the css cache and I needed to use drush cr again. Commented Aug 16, 2019 at 10:45

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.