When uploading images to the media library, WordPress will automatically generate several thumbnails so that a more optimized image size can be used.
Since different themes and plugins will often use different sizes, you might need to periodically regenerate your those resized images. In this article, we'll show you how to regenerate your image thumbnails using WP-CLI.
Regenerating All Thumbnails
To regenerate all thumbnails for all images inside your WordPress media library, you can use the following command:
wp media regenerate --yes
Once the command is run, all thumbnails for all images will be regenerated. Of course, if you have a large number of images inside your media library, it might take some extra time to run through them all.
Regenerating Missing Thumbnails
What if you updated your theme to add an additional image size but don't want to regenerate the thumbnails that already exist? You can regenerate thumbnails for only the missing image sizes using the --only-missing option like this:
wp media regenerate --only-missing
Regenerating Thumbnails for Specific Images
If you want to regenerate thumbnails for a specific image, you can specify it by including the attachment ID after the command like this:
wp media regenerate 123
To regenerate thumbnails for multiple specified attachment IDs, just add them to the command like the following example:
wp media regenerate 123 456 789
Keeping Old Thumbnail Sizes
When regenerating your WordPress media library image thumbnails, the default behavior is to delete the old thumbnails. If you want to keep them, you can use the --skip-delete option:
wp media regenerate --skip-delete
Regenerating Specific Thumbnail Sizes
In addition to being able to specify a specific image to regenerate thumbnails for, you can also define a specific thumbnail size to regenerate. To regenerate a specific size, use the --image_size option with the name of your thumbnail size as the value like in the following example:
wp media regenerate --image_size=large