Add wp media prune command to remove generated thumbnails#239
Add wp media prune command to remove generated thumbnails#239
wp media prune command to remove generated thumbnails#239Conversation
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
wp media prune command to remove generated thumbnails
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Pull request overview
This PR adds a new wp media prune subcommand to the media-command package to safely delete generated attachment thumbnails (optionally limited to specific registered sizes and/or including abandoned sizes) while updating attachment metadata so thumbnails can later be recreated via wp media regenerate.
Changes:
- Add
Media_Command::prune()command andprocess_prune()helper to remove intermediate image files and update attachment metadata. - Register the new
media prunecommand as bundled incomposer.json. - Add Behat acceptance coverage for pruning behavior (all images, specific sizes, abandoned-size handling, and invalid size errors).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/Media_Command.php |
Implements the new wp media prune command and per-attachment pruning logic. |
features/media-prune.feature |
Adds acceptance tests for prune scenarios and option behaviors. |
composer.json |
Registers media prune as a bundled WP-CLI command. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if ( file_exists( $intermediate_path ) ) { | ||
| unlink( $intermediate_path ); | ||
| } | ||
|
|
||
| $sizes_to_prune[] = $size_name; | ||
| } |
There was a problem hiding this comment.
The code unconditionally adds a size to $sizes_to_prune and unsets its metadata even if unlink() fails (and unlink() failures can also emit PHP warnings). Consider checking the return value of unlink() (and handling missing/undeletable files separately) so metadata is only removed when the file is successfully deleted (or already absent), and increment $errors / log a warning when deletion fails.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sites with many registered image sizes accumulate large numbers of generated thumbnails that can be safely deleted and later recreated via
wp media regenerate. No built-in WP-CLI command existed to do this selectively and safely.New command:
wp media pruneDeletes generated thumbnail files for registered image sizes and updates attachment metadata accordingly. Abandoned thumbnails (sizes no longer registered) are preserved by default since they cannot be regenerated.
Options
[<attachment-id>...]— limit to specific attachments; omitting prompts for confirmation[--image_size=<size>...]— limit to specific registered sizes (repeatable); validates against registered sizes[--remove-abandoned]— also remove thumbnails whose size is no longer registered[--yes]— bypass confirmation promptKey behaviours
-scaled) filewp media regeneratecorrectly identifies what needs to be rebuiltregenerate: batch processing, cache clearing every 500 items,report_batch_operation_results()outputChanges
src/Media_Command.php— addsprune()public command method andprocess_prune()private per-attachment helpercomposer.json— registersmedia pruneas a bundled commandfeatures/media-prune.feature— Behat acceptance tests covering: empty set, full prune, size-specific prune, abandoned-size preservation,--remove-abandoned, and invalid size errorOriginal prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.