get_results( "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key='_wp_attachment_metadata'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQLPlaceholders.UnnecessaryPrepare foreach ( $result as $row ) { $data = unserialize( $row->meta_value ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize if ( ! isset( $data['file'] ) ) { continue; } $file_path = $data['file']; $full_size_images[ $file_path ] = 1; } wp_cache_set( $cache_key, $full_size_images, false, 300 ); } $uploads_directory = wp_get_upload_dir()['basedir']; $years = array_diff( scandir( $uploads_directory ), [ '..', '.', '.DS_Store' ] ); foreach ( $years as $year ) { // skip unknown folders if ( ! is_numeric( $year ) ) { continue; } $year_directory = $uploads_directory . '/' . $year; foreach ( [ '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12' ] as $month ) { $month_directory = $year_directory . '/' . $month; // not all months have uploads if ( ! file_exists( $month_directory ) ) { continue; } $images = array_diff( scandir( $month_directory ), [ '..', '.', '.DS_Store' ] ); foreach ( $images as $image ) { $path = $year . '/' . $month . '/' . $image; $full_path = $month_directory . '/' . $image; if ( ! isset( $full_size_images[ $path ] ) ) { $total_rows++; if ( ! $dry_run ) { WP_CLI::line( "Deleting $full_path" ); unlink( $full_path ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_unlink } else { WP_CLI::line( "Found: $full_path" ); } } } } } // Print a success message if ( $total_rows ) { if ( $dry_run ) { WP_CLI::success( "Done. Found $total_rows files." ); } else { WP_CLI::success( "Done. Deleted $total_rows files." ); } } else { WP_CLI::success( 'Done. No files needed deleting.' ); } }