Table of Contents
The WooCommerce Nuclear Option: How to Wipe All Products and Media via functions.php
Sometimes, a “refresh” isn’t enough. Whether you’ve had a bulk import go horribly wrong or you are completely pivoting your E-Commerce store, deleting thousands of products manually through the WordPress dashboard is a recipe for a headache. You need a way to “nuke” the data directly from the Database.
At 3SixT5, we often handle massive store migrations. Our lead developer, Jp, uses custom snippets to ensure that when a client wants a clean slate, they get it—including the removal of orphaned images that usually clutter the Media Library.
Automating the deletion process isn’t just about saving time; it’s about database integrity. A clean wipe ensures no ‘ghost’ metadata is left behind to slow down your new store’s performance.
Jp
Owner & Developer
Where to Find your functions.php File
To use the code below, you need to access your theme’s functions.php file. You can find this in two places:
- WordPress Backend: Go to Appearance > Theme File Editor and select
functions.phpfrom the right-hand list. - Web Hosting File Manager: Log into your hosting (cPanel/Plesk), navigate to
public_html/wp-content/themes/[your-active-theme]/and look for thefunctions.phpfile.
The “Nuke” Snippet: Products, Images, Categories, and Tags
add_action( 'init', 'nuke_all_woo_data_and_media' );
function nuke_all_woo_data_and_media() {
// 1. Get all products (including variations)
$products = get_posts( array(
'post_type' => array( 'product', 'product_variation' ),
'post_status' => 'any',
'numberposts' => -1,
'fields' => 'ids',
));
foreach ( $products as $product_id ) {
$product = wc_get_product( $product_id );
if ( ! $product ) continue;
// A. Identify all images linked to this product
$image_ids = array();
// Get Featured Image
if ( $product->get_image_id() ) {
$image_ids[] = $product->get_image_id();
}
// Get Gallery Images
$gallery_ids = $product->get_gallery_image_ids();
if ( ! empty( $gallery_ids ) ) {
$image_ids = array_merge( $image_ids, $gallery_ids );
}
// B. Delete the images from Media Library (disk + database)
foreach ( array_unique( $image_ids ) as $media_id ) {
wp_delete_attachment( $media_id, true );
}
// C. Delete the product itself
wp_delete_post( $product_id, true );
}
// 2. Delete Categories and Tags
$taxonomies = array( 'product_cat', 'product_tag' );
foreach ( $taxonomies as $taxonomy ) {
$terms = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => false, 'fields' => 'ids' ) );
foreach ( $terms as $term_id ) {
wp_delete_term( $term_id, $taxonomy );
}
}
}Why This is Better Than Manual Deletion
When you delete a product in the dashboard, WordPress often leaves the images behind in the Media Library. This results in “Digital Bloat,” wasting server space and making your site harder to manage. Jp’s code ensures that every Digital Asset tied to the product is scrubbed clean from the server disk.
Need Expert E-Commerce Help?
- Need a professional to handle your migration? See our E-Commerce Solutions.
- Looking for a fresh look after your wipe? Check out Duduza’s Custom Design.
- Ensure your new store is visible with Thinus’s 13-Year SEO Expertise.
Final Step: Remove the Code
Once you have refreshed your homepage once, all your WooCommerce data will be gone. You must remove this code from your functions.php immediately. If you leave it in, you will never be able to add new products because the site will delete them every time it loads!
