ideas icon indicating copy to clipboard operation
ideas copied to clipboard

Media command to remove all generated images

Open desrosj opened this issue 6 years ago • 5 comments

Today I was cleaning up a hosting account for a freelance client by backing up and deleting sites that they no longer use. My typical process for this is to create a zip of the site's final state (including a database export and uploads directory) and tucking it away for safe keeping for a period of time.

One site had a very large uploads directory because of a large number of registered image sizes. This meant a very large ZIP file. However, after removing the generated image sizes leaving only the original, the ZIP file backup was significantly smaller. If I ever need to spin up this site again for whatever reason, I can just restore the database and run wp media regenerate, and all of the image sizes would be present.

I used find . -name "*-*x*.*" | xargs rm -f to accomplish this (props here), but this could remove original files that happen to follow a -400x400. naming pattern. It would be great if there was a command that:

  • Grabbed the list of registered image sizes.
  • Looked at each attachment and removed any generated image that matches a registered image size.
  • Deleted all of these matches from the uploads directory.

Abandoned image sizes (images generated and stored in attachment meta that no longer correspond to a registered image size) would need to remain because those cannot be regenerated, but still may be linked in content.

Almost a reverse regenerate-thumbnails command.

desrosj avatar Nov 01 '19 01:11 desrosj

I think it makes sense to add a command like this.

However, it should be noted that there's no guarantee that if you regenerate the thumbnails, that you'll get the exact same thumbnails with the same settings and the same quality:

  • WordPress can use different image libraries depending on what the server supports. These have differing results.
  • Color correction could be misconfigured on the server, resulting in different colors in the thumbnails.
  • The PHP configuration for the image sizes might have been changed without rebuilding the thumbnails, so some/all of the thumbnails might not actually reflect the current configuration.
  • Plugins (active, inactive or uninstalled ones) might have been used to adapt the thumbnail generation.
  • The plugins might have been generated via an external service which might or might not be available from the target location.

This is a destructive operation, and although there is a good probability that regeneration will recreate what was deleted, there's no guarantee for that.

schlessera avatar Jan 28 '20 14:01 schlessera

Analogous to regenerate, this could be called prune.

It could have an optional flag --remove-abandoned to get rid of thumbnails that can't be regenerated anymore.

schlessera avatar Jan 28 '20 14:01 schlessera

I have some CLI code that deletes all intermediate images. Would that be of interest?

paulschreiber avatar Mar 31 '20 20:03 paulschreiber

@paulschreiber I am interested, as I have a need for this.

amooreTO avatar May 20 '22 18:05 amooreTO

@amooreTO https://gist.github.com/paulschreiber/a8109ee4afa76f45708c8b980159c9bd

paulschreiber avatar May 20 '22 23:05 paulschreiber