Delete Image Size Files No Longer Registered
Adds Tools > Unused Image Sizes, which finds resized copies of your images made for sizes your current theme and plugins no longer register, shows how much space they take, and deletes them in batches after confirmation.
Opens WPIDE on
Code
add_action( 'admin_menu', function () {
add_management_page( 'Unused Image Sizes', 'Unused Image Sizes', 'manage_options', 'wpide-unused-sizes', function () {
$registered = array_merge( get_intermediate_image_sizes(), array( 'full', 'original_image' ) );
$uploads = wp_get_upload_dir();
$delete = isset( $_POST['wpide_delete_sizes'] ) && check_admin_referer( 'wpide_unused_sizes' ) && current_user_can( 'manage_options' );
$offset = $delete ? 0 : ( isset( $_GET['offset'] ) ? absint( $_GET['offset'] ) : 0 );
$found = array();
$bytes = 0;
$deleted = 0;
$ids = get_posts( array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => 'inherit',
'posts_per_page' => 300,
'offset' => $offset,
'fields' => 'ids',
'no_found_rows' => true,
) );
foreach ( $ids as $id ) {
$meta = wp_get_attachment_metadata( $id );
if ( empty( $meta['sizes'] ) || empty( $meta['file'] ) ) {
continue;
}
$dir = trailingslashit( $uploads['basedir'] ) . trailingslashit( dirname( $meta['file'] ) );
$changed = false;
foreach ( $meta['sizes'] as $size => $data ) {
if ( in_array( $size, $registered, true ) || empty( $data['file'] ) ) {
continue;
}
$path = $dir . $data['file'];
$found[ $size ] = ( isset( $found[ $size ] ) ? $found[ $size ] : 0 ) + 1;
if ( file_exists( $path ) ) {
$bytes += filesize( $path );
if ( $delete && wp_delete_file_from_directory( $path, $uploads['basedir'] ) ) {
$deleted++;
}
}
if ( $delete ) {
unset( $meta['sizes'][ $size ] );
$changed = true;
}
}
if ( $changed ) {
wp_update_attachment_metadata( $id, $meta );
}
}
echo '<div class="wrap"><h1>Unused Image Sizes</h1>';
if ( $delete ) {
printf( '<div class="notice notice-success"><p>%s</p></div>', esc_html( sprintf( 'Deleted %d file(s), freeing %s. Run again to process more images.', $deleted, size_format( $bytes, 1 ) ) ) );
}
printf( '<p>%s</p>', esc_html( sprintf( 'Checked %d images (starting at #%d). Sizes registered now: %s.', count( $ids ), $offset, implode( ', ', get_intermediate_image_sizes() ) ) ) );
if ( $found && ! $delete ) {
echo '<table class="widefat striped" style="max-width:520px"><thead><tr><th>Unregistered size</th><th>Files</th></tr></thead><tbody>';
foreach ( $found as $size => $count ) {
printf( '<tr><td><code>%s</code></td><td>%d</td></tr>', esc_html( $size ), (int) $count );
}
printf( '</tbody></table><p>%s</p>', esc_html( 'Space used by these files: ' . size_format( $bytes, 1 ) ) );
echo '<form method="post" onsubmit="return confirm(\'Delete these resized files? Originals are kept.\');">';
wp_nonce_field( 'wpide_unused_sizes' );
submit_button( 'Delete unused size files in this batch', 'primary', 'wpide_delete_sizes' );
echo '</form>';
} elseif ( ! $delete ) {
echo '<p>No unused size files in this batch.</p>';
}
if ( count( $ids ) === 300 && ! $delete ) {
echo '<p><a class="button" href="' . esc_url( add_query_arg( 'offset', $offset + 300 ) ) . '">Check the next 300 images</a></p>';
}
echo '</div>';
} );
} );
Notes
Pages that still hard-code a removed size will fall back to a larger size; back up your uploads folder first.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.