Programmatically Create & Delete Image Styles in Drupal

Need to programmatically create or delete an image style in Drupal? Maybe you're creating a module that needs to define a new style or looking to use an update handler to delete one. It's a cinch to do with just a few lines of code.
Published
Updated
Typical Read
2 minutes
Sponsored
Need help with website, a boost in ranking or online marketing? Contact me today for a free quote.
Find your next web developer job
jobs by Indeed

Programmatically Create & Delete Image Styles in Drupal was originally published on May 21, 2014 and has recently been updated on Aug 6, 2020 to reflect emerging trends.

I recently needed to delete a image style that was no longer being used with a update handler. Being that Drupal’s documentation is less than informative, I did some digging and finally figured it out. Check out how to create, flush and delete image styles below.

Programmatically Create an Image Style

The code below will create an image style named, avatar that will scale images to 64×64:

<?php
$style = image_style_save(array('name' => 'avatar'));
$effect = array(
  'name' => 'image_scale',
  'data' => array(
    'width' => 64,
    'height' => 64,
    'upscale' => TRUE,
  ),
  'isid' => $style['isid'],
);
image_effect_save($effect);

Or, you can create image styles with a Drupal hook using hook_image_default_styles:

<?php
function hook_image_default_styles() {
  $styles = array();
  $styles['avatar'] = array(
    'effects' => array(
      array(
        'name' => 'image_scale',
        'data' => array(
          'width' => 200,
          'upscale' => 1,
        ),
        'weight' => 0,
      ),
      array(
        'name' => 'image_crop',
        'data' => array(
          'width' => 200,
          'height' => 200,
        ),
        'weight' => 1,
      ),
    ),
  );

  return $styles;
}

Programmatically Delete an Image Style

<?php
image_style_delete(image_style_load('my_image_style'));

Don’t forget to flush…

You may need to flush the image style before Drupal will let you delete it. You can flush image styles using two different methods:

Flush Image Styles with Drupal Console

# List image styles on the site
$ drupal image:styles:debug

# Execute flush function by image style or execute all flush images styles
$ drupal image:styles:flush

For a list of other useful Drupal 8 Console commands, see benmarshall.me/drupal-8-console-commands/.

Flush image styles with Drush

$ drush image-flush

This will create a list of options with all of your defined image styles. For more information run:

$ drush help image-flush

Programmatically flush image styles

<?php
image_style_flush('STYLE_NAME');

Or, if you want to flush all image styles:

<?php
foreach (image_styles() as $style) {
  image_style_flush($style);
}

More on Drupal Image Styles

Did you find Programmatically Create & Delete Image Styles in Drupal useful? Get articles in your inbox.

…and don’t worry, I hate spam as much as you. Expect to hear from me at most once a week.

Latest Job Postings
Posted on Aug 9, 2020 at 9:17pm
Full-time
Los Angeles
Posted on Aug 9, 2020 at 9:15pm
Full-time
Los Angeles
Posted on Aug 9, 2020 at 1:19am
Littleton, CO
jobs by Indeed
Sponsored
Do you run a development agency, freelance developer or offer website services. Contact me today for information about advertising.
Sponsored
Help support open-source projects & pro-bono non-profit services by donating today.
Sponsored
Do you have a great article to share you want to contribute? Contact me about your idea.
Sponsored
Need help with website, a boost in ranking or online marketing? Contact me today for a free quote.
Sponsored
Need help with website, a boost in ranking or online marketing? Contact me today for a free quote.

1 Comment on “Programmatically Create & Delete Image Styles in Drupal”

Q__nt_n

# Jan 19, 2018

To flush all image_style in D8

$imageStyle = \Drupal::entityTypeManager()->getStorage(‘image_style’);
$styles = $imageStyle->loadMultiple();
foreach ($styles as $style) {
$imageStyle->load($style->get(‘name’))->flush();
}

Leave a Reply

Your email address will not be published. Required fields are marked *

All comments posted on 'Programmatically Create & Delete Image Styles in Drupal' are held for moderation and only published when on topic and not rude. Get a gold star if you actually read & follow these rules.

You may write comments in Markdown. This is the best way to post any code, inline like `<div>this</div>` or multiline blocks within triple backtick fences (```) with double new lines before and after.

Want to tell me something privately, like pointing out a typo or stuff like that? Contact Me.