Bulk Delete WooCommerce Out of Stock Products

Sometimes we need to delete tons of out of stock products that we dont sell and we dont want sell anymore. If that is the case this code adds a menu page to your wp-admin and adds a button to delete all out of stock products.

WARNING this deletes the products completely doesnt even sends it to trash it just deletes it completely!

If you are not sure what you are doing dont do it !

I used this couple of times with big chunks of products like 3000 or 8000 products it finished under a minute. But it was a fast hetzner server.



add_action('admin_menu', function() {
    add_menu_page(
        'Delete Out-of-Stock Products', // Page title
        'Delete Out-of-Stock Products', // Menu title
        'manage_options',               // Capability required to access
        'delete-out-of-stock-products', // Menu slug
        'render_delete_out_of_stock_page', // Callback function to render the page
        'dashicons-trash',              // Icon (optional)
        20                              // Position (optional)
    );
});


function render_delete_out_of_stock_page() {
    ?>
    <div class="wrap">
        <h1>Delete All Out-of-Stock Products</h1>
        <p>Click the button below to delete all products that are currently out of stock. This action cannot be undone, so please proceed with caution.</p>
        <form method="post" action="">
            <?php
            // Output security fields for the form
            wp_nonce_field('delete_out_of_stock_action', 'delete_out_of_stock_nonce');
            ?>
            <input type="submit" name="delete_out_of_stock" class="button button-primary" value="Delete Out-of-Stock Products" onclick="return confirm('Are you sure you want to delete all out-of-stock products? This action cannot be undone.');">
        </form>
    </div>
    <?php
}


add_action('admin_init', function() {
    // Check if the form has been submitted
    if (isset($_POST['delete_out_of_stock'])) {
        // Verify the nonce for security
        check_admin_referer('delete_out_of_stock_action', 'delete_out_of_stock_nonce');

        // Ensure the user has the required capability
        if (current_user_can('manage_options')) {
            // Query for out-of-stock products
            $args = [
                'post_type'      => 'product',
                'posts_per_page' => -1,
                'meta_query'     => [
                    [
                        'key'   => '_stock_status',
                        'value' => 'outofstock',
                    ],
                ],
            ];

            $out_of_stock_products = get_posts($args);

            if (empty($out_of_stock_products)) {
                add_action('admin_notices', function() {
                    echo '<div class="notice notice-info is-dismissible">
                        <p>No out-of-stock products found.</p>
                    </div>';
                });
            } else {
                // Delete each out-of-stock product
                foreach ($out_of_stock_products as $product) {
                    wp_delete_post($product->ID, true);
                }

                add_action('admin_notices', function() {
                    echo '<div class="notice notice-success is-dismissible">
                        <p>All out-of-stock products have been successfully deleted.</p>
                    </div>';
                });
            }
        } else {
            add_action('admin_notices', function() {
                echo '<div class="notice notice-error is-dismissible">
                    <p>You do not have sufficient permissions to perform this action.</p>
                </div>';
            });
        }
    }
});