Member-only story

Flush All Image Styles in Drupal 9

An easy way to do it programmatically

Photo by Héctor J. Rivas on Unsplash

In Drupal, image styles are used to create different sizes and formats of images for use across a website. Sometimes, it may be necessary to clear or flush all image styles, for example, after making changes to image processing settings or when migrating content from one site to another.

This can be normally done using the command: drush image:flush — all, but not all servers have drush or maybe you just want to clear them when deploying automatically without having to run any command.
This can be done programmatically using this code:

/**
* Clear all image styles programmatically.
* @param $sandbox
*/
function hook_update_9001(&$sandbox) {
$image_styles = \Drupal\image\Entity\ImageStyle::loadMultiple();
foreach ($image_styles as $style) {
$style->flush();
}
}

👉 Find out more about me here: https://sergioguardiola.net 🔥

--

--

Sergio Guardiola Herrador
Sergio Guardiola Herrador

Written by Sergio Guardiola Herrador

I write articles in English and Spanish, mostly about programming, technology, travel, money, investing. You can find me here: https://sergioguardiola.net

No responses yet