The WooCommerce Nuclear Option: How to Wipe All Products and Media via functions.php

How to Wipe All Products and Media via functions.php

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.php from 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 the functions.php file.

The “Nuke” Snippet: Products, Images, Categories, and Tags

⚠️ CRITICAL WARNING: This code is destructive and permanent. Perform a full backup of your site and Database before adding this. Once you save the file and refresh your site once, delete this code immediately, or your site will continue to delete all incoming data on every page load!
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?

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!

The WooCommerce Nuclear Option: How to Wipe All Products and Media via functions.php

Comments? Leave them below