Title: SQL command to delete unused image sizes?
Last modified: September 26, 2021

---

# SQL command to delete unused image sizes?

 *  [joycegrace](https://wordpress.org/support/users/joycegrace/)
 * (@joycegrace)
 * [4 years, 8 months ago](https://wordpress.org/support/topic/sql-command-to-delete-unused-image-sizes/)
 * Hello, I’m wondering if someone can tell me if there is a command or query in
   SQL to delete the unused image sizes in WordPress?
 * To give some background:
 * We have thousands of images. Many are duplicates of sizes we don’t need in WordPress,
   like medium-large, extra large and scaled, etc. These are generated by WordPress
   core. They are not from themes and plugins. But even if they were, we’d also 
   not need these extra files.
 * I would like to delete ONLY the image size variations we don’t need.
 * I see plugins to delete whole images that are unused on posts or pages. This 
   is not what I’m looking for. I don’t want to delete all images we are not using(
   i.e. images may still be uploaded or attached to a post – we don’t want to delete
   the image size variations we DO need, just the ones we DON’T need).
 * How would we find a reference of what command or query to use to delete these
   specific images in the file system and database?
 * Or does someone know what works already and has tried this?
 * I saw this:
 * [https://developer.wordpress.org/cli/commands/media/regenerate/](https://developer.wordpress.org/cli/commands/media/regenerate/)
 * But I don’t want to regenerate (unless I’m wrong about this). I want to remove
   those sizes forever so they are not taking up server space. Then I want the site
   to default to the remaining image sizes that are available.
 * Thank you.
    -  This topic was modified 4 years, 8 months ago by [Jan Dembowski](https://wordpress.org/support/users/jdembowski/).
      Reason: Moved to Fixing WordPress, this is not an Developing with WordPress
      topic

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [Kuldeep](https://wordpress.org/support/users/soberbanda/)
 * (@soberbanda)
 * [4 years, 8 months ago](https://wordpress.org/support/topic/sql-command-to-delete-unused-image-sizes/#post-14908939)
 * [@joycegrace](https://wordpress.org/support/users/joycegrace/) Grab a coffee 
   and go through this: [https://wordpress.stackexchange.com/questions/240765/how-to-delete-resized-cropped-image-uploads-and-prevent-future-resizing](https://wordpress.stackexchange.com/questions/240765/how-to-delete-resized-cropped-image-uploads-and-prevent-future-resizing)
 * Let me know if it helped 🙂
 *  [rickymccallum87](https://wordpress.org/support/users/rickymccallum87/)
 * (@rickymccallum87)
 * [4 years, 8 months ago](https://wordpress.org/support/topic/sql-command-to-delete-unused-image-sizes/#post-14909172)
 * The regenerate command will do what you want. You just need to remove your unwanted
   image sizes before you run it.
 * There are a few different methods needed to get at all of the image sizes that
   core has put in place. This code snippet removes medium_large, 1536×1536, 2048
   ×2048, and “-scaled”.
 *     ```
       add_filter('intermediate_image_sizes', 'wpsf_remove_medium_large_image_size');
       function wpsf_remove_medium_large_image_size($sizes)
       {
         return array_diff($sizes, array('medium_large'));
       };
   
       add_action('init', 'wpsf_remove_large_image_sizes');
       function wpsf_remove_large_image_sizes()
       {
         remove_image_size('1536x1536');
         remove_image_size('2048x2048');
       }
   
       add_filter('big_image_size_threshold', '__return_false');
       ```
   
 * With that in place, new uploads to the media library will not have those image
   sizes generated. And you’re ready to delete the unwanted sizes that are already
   sitting on your server. I recommend the “delete old unregistered sizes” feature
   of the [Regenerate Thumbnails](https://wordpress.org/plugins/regenerate-thumbnails/)
   plugin.
 * What’s nice about the plugin, as opposed to the WP CLI regenerate command, is
   that it reports the currently active sizes in a list so that you can check that
   the above code is working before you regenerate. And, of course, you should take
   a backup of your uploads folder beforehand.
 *  Thread Starter [joycegrace](https://wordpress.org/support/users/joycegrace/)
 * (@joycegrace)
 * [4 years, 8 months ago](https://wordpress.org/support/topic/sql-command-to-delete-unused-image-sizes/#post-14918480)
 * [@rickymccallum87](https://wordpress.org/support/users/rickymccallum87/) your
   solution worked, thank you so much!
 * I was actually using this solution to remove unused image sizes [https://advent.elliottrichmond.co.uk/how-to-disable-all-unwanted-image-sizes/](https://advent.elliottrichmond.co.uk/how-to-disable-all-unwanted-image-sizes/)
 * But I didn’t realize that I could automatically delete them if they were already
   created and sitting on the server.
 * This was a big help, thank you very much.
 *  [rickymccallum87](https://wordpress.org/support/users/rickymccallum87/)
 * (@rickymccallum87)
 * [4 years, 8 months ago](https://wordpress.org/support/topic/sql-command-to-delete-unused-image-sizes/#post-14921902)
 * You’re welcome!
 * Thank you for sharing that post. Elliott’s code snippet is cleaner. I’ve reproduced
   it here for posterity.
 *     ```
       // Disable generated image sizes
       function wpsf_disable_image_sizes($sizes) {
           unset($sizes['thumbnail']);
           unset($sizes['medium']);
           unset($sizes['large']);
           unset($sizes['medium_large']);
           unset($sizes['1536x1536']);
           unset($sizes['2048x2048']);
           return $sizes;
       }
       add_action('intermediate_image_sizes_advanced', 'wpsf_disable_image_sizes');
   
       // Disable scaled image size
       add_filter('big_image_size_threshold', '__return_false');
       ```
   
 * Although if one is using the Regenerate Thumbnails plugin, I recommend [my version](https://wordpress.org/support/topic/sql-command-to-delete-unused-image-sizes/#post-14909172).
   It makes the plugin’s sizes listing match what will happen when pressing the 
   Regenerate Thumbnails button.
 *  Thread Starter [joycegrace](https://wordpress.org/support/users/joycegrace/)
 * (@joycegrace)
 * [4 years, 8 months ago](https://wordpress.org/support/topic/sql-command-to-delete-unused-image-sizes/#post-14953096)
 * Very good to know, thank you.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘SQL command to delete unused image sizes?’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 3 participants
 * Last reply from: [joycegrace](https://wordpress.org/support/users/joycegrace/)
 * Last activity: [4 years, 8 months ago](https://wordpress.org/support/topic/sql-command-to-delete-unused-image-sizes/#post-14953096)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
