Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically delete an image style in Drupal?

Need help trying to figure out how to programmatically delete an image style in Drupal 7.

like image 514
Ben Marshall Avatar asked May 21 '14 18:05

Ben Marshall


2 Answers

$image_style = image_style_load('IMAGE_STYLE');
image_style_delete($image_style);

See https://api.drupal.org/api/drupal/modules!image!image.module/function/image_style_delete/7

You may need to flush the image style cache before it'll let you delete it. You can do that with:

image_style_flush($image_style);

For more info, see:

  1. https://api.drupal.org/api/drupal/modules!image!image.module/function/image_style_flush/7
  2. http://www.benmarshall.me/programmatically-create-delete-image-styles-drupal/
like image 140
Ben Marshall Avatar answered Nov 09 '22 20:11

Ben Marshall


<?php
// Load the style.
$style = image_style_load('styleName');
// Flush images associated with the style.
image_style_flush($style);
// Delete the style.
image_style_delete($style);
?>

This will not work on the default styles. It is recommended to not remove them, since a number of Drupal modules depends on it.

Update:

If you've modified the default styles (Thumbnail, Medium, Large), you cannot delete them. Instead, you can just revert them back to their original settings, like this.

<?php
// Load the style.
$style = image_style_load('styleName');
// Flush images associated with the style.
image_style_flush($style);
// Revert the style.
image_default_style_revert($style);
?>
like image 24
timofey.com Avatar answered Nov 09 '22 18:11

timofey.com