woocommerce offtopic – Is it safe to delete from db orphaned posts i.e. whose post_parent no longer exists?


I inherited a couple of WP + WooCommerce shops with roughly 30,000 products for sale each, and a wp_postmeta which is over a million lines. The former webmasters ran away.

In an effort to clean up old stuff, I noticed a post_parent field in wp_posts, and queried the database for orphans:

SELECT ID FROM wp_posts WHERE NOT post_parent IN 
  (SELECT ID FROM wp_posts) AND post_parent>0 

and found thousands of records.
All of these records have a post_parent which no longer exists.

Just out of curiosity, I checked wp_postmeta:

SELECT * FROM wp_postmeta where post_id in
  (SELECT ID FROM wp_posts where not post_parent in 
     (select ID from wp_posts) and post_parent>0 ) 

and found 60,000 records.

Is it safe to delete them, along with any references from the tables wp_postmeta, wp_comments, wp_commentmeta, wp_term_relationships, wp_wc_product_meta_lookup ?

Else, can you suggest a strategy to clean up the database from spurious data?

Riccardo Zorn 2 years 2021-04-09T13:48:49-05:00 0 Answers 0 views 0

Leave an answer