I have a new set of image sizes for my site’s in-development theme. My server is filled with old image sizes, most of which have never been used. You’ve heard this one before, right?
The Question
How do I delete all WP generated thumbnails from my server but not those thumbnails currently used in posts?
That is to say something like: $ find . -name "*-*x*.jpg" | xargs rm -f
is too wide of a hammer because myimage-300x250.jpg
might be used in a post. Its 6 other variations are welcome to go away though!
I think I need to generate a list of used images, compare that to my directory and delete the difference (minus the originals). But how to go about that escapes me.
The “why?” sidebar:
Extra image sizes on the server is generally no big deal but we have over 2.7 gigs in their right now and it just seems a waste to leave them and back them up. Plus, we’re about to start fresh – why not do the spring cleaning?
As far as regenerating the thumbs, while the new design has new image specifications, none of us have the time to go and update hundreds of older posts with RTE inserted images. Let bygones be bygones, I say.
This should get you started:
Might need to batch it or boost the timeout & memory limit, depending on the server & number of posts you’re working with.
What you do with this is up to you. If you’re going to keep the original but delete size variations, make sure you wipe it’s existence from the database entry in
$meta['sizes']
and save withwp_update_attachment_metadata( $post_id, $meta )
.You could even go so far as to regenerate the new sizes with
wp_get_image_editor()
& update the oldsrc
‘s in your post content on-the-fly.