Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mrizwan47/54bee12f3a89440867c45490f13dbe74 to your computer and use it in GitHub Desktop.
Save mrizwan47/54bee12f3a89440867c45490f13dbe74 to your computer and use it in GitHub Desktop.
Remove all WooCommerce products from database via SQL
<?php
global $wpdb;
$wpdb->query("DELETE FROM $wpdb->terms WHERE term_id IN (SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy LIKE 'pa_%')");
$wpdb->query("DELETE FROM $wpdb->term_taxonomy WHERE taxonomy LIKE 'pa_%'");
$wpdb->query("DELETE FROM $wpdb->term_relationships WHERE term_taxonomy_id not IN (SELECT term_taxonomy_id FROM $wpdb->term_taxonomy)");
$wpdb->query("DELETE FROM $wpdb->term_relationships WHERE object_id IN (SELECT ID FROM $wpdb->posts WHERE post_type IN ('product','product_variation'))");
$wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id IN (SELECT ID FROM $wpdb->posts WHERE post_type IN ('product','product_variation'))");
$wpdb->query("DELETE FROM $wpdb->posts WHERE post_type IN ('product','product_variation')");
$wpdb->query("DELETE pm FROM $wpdb->postmeta pm LEFT JOIN $wpdb->posts wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment