I've just inherited a site that's being migrated to a new host, and I'm attempting to tidy up the database during the process.

The WooCommerce site had roughly 10,000 products resulting in somewhere around 34,000 product images.

The site also had an active blog, and a lot of specialized pages with non-product images.

I have wiped all the products out of the db, but I'm stuck trying to figure out how to erase all images attached to _product type posts, as well as wiping their records from the database.

End goal is to permanently delete all product images from the media library as well as the database.

Any help would be much appreciated.

1 Answer 1


You can loop through all the woo-commerce products and delete them along with their attachment(images)..

$args = array( 'post_type' => 'product', ... );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php wp_delete_post( $loop->post->ID ); 
wp_delete_attachment( $loop->post->attachment_id ); ?>
<?php endwhile; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.