Wednesday, July 2, 2025

SQL to delete all WooCommerce products except those with "draft" status

 -- Disable foreign key checks (important for InnoDB)

SET

    FOREIGN_KEY_CHECKS = 0;

    -- Delete product meta

DELETE

    pm

FROM

    wp_postmeta pm

JOIN wp_posts p ON

    pm.post_id = p.ID

WHERE

    p.post_type = 'product' AND p.post_status != 'draft';

    -- Delete term relationships

DELETE

    tr

FROM

    wp_term_relationships tr

JOIN wp_posts p ON

    tr.object_id = p.ID

WHERE

    p.post_type = 'product' AND p.post_status != 'draft';

    -- Delete the products

DELETE

FROM

    wp_posts

WHERE

    post_type = 'product' AND post_status != 'draft';

    -- Enable foreign key checks again

SET

    FOREIGN_KEY_CHECKS = 1;

No comments:

Post a Comment

SQL to delete all WooCommerce products except those with "draft" status

 -- Disable foreign key checks (important for InnoDB) SET     FOREIGN_KEY_CHECKS = 0;     -- Delete product meta DELETE     pm FROM     wp_p...