Last active
September 19, 2024 08:39
-
-
Save chandrapatel/84ee4302624580cc407731881a7214aa to your computer and use it in GitHub Desktop.
WP-CLI command to delete all the thumbnails.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* WP-CLI command to delete all the thumbnails. | |
* | |
* Note: First do dry-run, verify log and then do actual run. | |
* | |
* @package delete-thumbnails | |
*/ | |
if ( defined( 'WP_CLI') && WP_CLI ) { | |
class WP_Delete_Thumbnails { | |
/** | |
* Delete all the thumbnails | |
* | |
* ## OPTIONS | |
* | |
* [--dry-run] | |
* : Whether or not to do dry run | |
* --- | |
* default: false | |
* options: | |
* - true | |
* - false | |
* | |
* ## EXAMPLES | |
* | |
* wp delete-thumbnails --dry-run | |
* wp delete-thumbnails | |
* | |
* @param array $args Positional arguments. | |
* @param array $assoc_args Associative arguments. | |
*/ | |
public function __invoke( $args, $assoc_args ) { | |
if ( isset( $assoc_args['dry-run'] ) && ( true === $assoc_args['dry-run'] || 'true' === $assoc_args['dry-run'] ) ) { | |
$dry_run = true; | |
} else { | |
$dry_run = false; | |
} | |
// Starting time of the script. | |
$start_time = time(); | |
if ( $dry_run ) { | |
WP_CLI::line( 'Delete thumbnails command dry-run started.' ); | |
} else { | |
WP_CLI::line( 'Delete thumbnails command started.' ); | |
} | |
$offset = 0; | |
$batch_size = 100; | |
$total_attachments = 0; | |
$total_images = 0; | |
$total_fail = 0; | |
$total_success = 0; | |
$query_args = array( | |
'offset' => $offset, | |
'post_type' => 'attachment', | |
'posts_per_page' => $batch_size, | |
'post_status' => 'any', | |
'fields' => 'ids', | |
); | |
$query = new \WP_Query( $query_args ); | |
do { | |
$query->set( 'offset', $offset ); | |
$results = $query->get_posts(); | |
if ( empty( $results ) ) { | |
break; | |
} | |
$offset += $batch_size; | |
$count = count( $results ); | |
$total_attachments += $count; | |
foreach ( $results as $attachment_id ) { | |
// Continue if attachment is not image. | |
if ( ! wp_attachment_is_image( $attachment_id ) ) { | |
continue; | |
} | |
WP_CLI::line( "\n" . 'Deleting thumbnails for attachment ID: ' . $attachment_id ); | |
$total_images++; | |
$metadata = wp_get_attachment_metadata( $attachment_id ); | |
if ( empty( $metadata['sizes'] ) || ! is_array( $metadata['sizes'] ) ) { | |
WP_CLI::warning( 'Thumbnails does not found.' ); | |
} | |
if ( function_exists( 'wp_get_original_image_path' ) ) { | |
$original_image_dir_path = wp_get_original_image_path( $attachment_id ); | |
} | |
if ( false !== $original_image_dir_path ) { | |
$original_image_dir_path = get_attached_file( $attachment_id ); | |
} | |
$original_image_dir_path = trailingslashit( dirname( $original_image_dir_path ) ); | |
$sizes = $metadata['sizes']; | |
foreach ( $sizes as $size_name => $size_info ) { | |
if ( empty( $size_info['file'] ) ) { | |
WP_CLI::warning( sprintf( 'Thumbnail file of %s size does not found in metadata.', $size_name ) ); | |
continue; | |
} | |
$thumbnail_path = $original_image_dir_path . $size_info['file']; | |
if ( ! file_exists( $thumbnail_path ) ) { | |
WP_CLI::warning( sprintf( 'Thumbnail file of %s size does not exists.', $size_name ) ); | |
continue; | |
} | |
if ( $dry_run ) { | |
WP_CLI::success( sprintf( 'Thumbnail file of %s size will be deleted.', $size_name ) ); | |
$total_success++; | |
continue; | |
} | |
if ( unlink( $thumbnail_path ) ) { | |
WP_CLI::success( sprintf( 'Thumbnail file of %s size deleted successfully.', $size_name ) ); | |
unset( $metadata['sizes'][ $size_name ] ); | |
$total_success++; | |
} else { | |
WP_CLI::warning( sprintf( 'Thumbnail file of %s size failed to delete.', $size_name ) ); | |
$total_fail++; | |
continue; | |
} | |
} | |
if ( ! $dry_run ) { | |
if ( wp_update_attachment_metadata( $attachment_id, $metadata ) ) { | |
WP_CLI::success( 'Attachment metadata updated successfully.' ); | |
} else { | |
WP_CLI::success( 'Attachment metadata does not updated.' ); | |
} | |
} else { | |
WP_CLI::success( 'Attachment metadata will be updated.' ); | |
} | |
} | |
sleep( 1 ); | |
} while ( $count === $batch_size ); | |
WP_CLI::line( "\nSummary:" ); | |
WP_CLI::line( sprintf( 'Total attachments : %d', $total_attachments ) ); | |
WP_CLI::line( sprintf( 'Total images attachments : %d', $total_attachments ) ); | |
if ( $dry_run ) { | |
WP_CLI::line( sprintf( 'Total thumbnails will be deleted : %d', $total_success ) ); | |
} else { | |
WP_CLI::line( sprintf( 'Total thumbnails deleted : %d', $total_success ) ); | |
} | |
WP_CLI::line( sprintf( 'Total thumbnails does not deleted : %d', $total_fail ) ); | |
WP_CLI::line( "\n" . sprintf( 'Total time taken to delete all thumbnails: %s', human_time_diff( $start_time, time() ) ) ); | |
} | |
} | |
WP_CLI::add_command( 'delete-thumbnails', 'WP_Delete_Thumbnails' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment