Search code examples
woocommercebulk-delete

Woocommerce - Need to delete products with no association to a categories with SQL or plugin


Background: I inherited a website with a tremendously bloated product database. There are around 260 actual products which are translated using WPML into English and French which should make it for a total of 520 posts (for each product there is one same SKU two different IDs). However the total count of products in published status is over 720.

I exported all my products and realized that there is a third instance of some products with the same SKU and yet another ID but they are not associated to any category i.e The 'Categories' field is empty. These are not variants are just ghost instances of the parent products.

Problem to solve:

  • I need to get rid of those 'ghost' products that have no Categories association. (I do have the list of offender IDs as a CSV)
  • If possible I would like to understand what those extra products. Can they be untranslated versions of the listing? are they just corrupted/invalid records?
  • I can do SQL or plugin or whatever it takes.

Difference in Product Count that shows invalid products

Thanks!


Solution

  • I've solved the problem by using the CLI shell in WordPress.

    By simply SSHing into the backend I used the following syntax

    wp wc product delete <ID> for trashing the product and you can add the --force to permanently delete it.

    Having the list of offender IDs I just simply wrote a shell script that executed all the commands sequentially.