Skip to content

Media command to remove all generated images #238

Description

@desrosj

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.

Activity

  1. schlessera commented on Jan 28, 2020

    @schlessera
    Member

    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.

  2. schlessera commented on Jan 28, 2020

    @schlessera
    Member

    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.

  3. paulschreiber commented on Mar 31, 2020

    @paulschreiber

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

  4. amooreTO commented on May 20, 2022

    @amooreTO

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

  5. transferred this issue fromwp-cli/ideason Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions