At work, we got ourselves a nice WooCommerce shop. Using WP All Import, it automagically imports the products and updates stock. But somehow in we ended up with products in our database with no SKU.
Removing one of those products, and then rerun the import, seemed to work. So maybe these products where in the product feed once without a SKU. Who knows. Anyways, I wanted them gone, and didn’t want to delete the entire database, afraid of what Google may think of that.
So I checked the database a bit and came up with a SQL query. First, to check if you have products with empty SKU’s as well, you could run something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
SELECT p.ID, p.post_title, p.post_type, pm.meta_value FROM wp_posts p LEFT JOIN wp_postmeta pm ON pm.post_id = p.ID AND pm.meta_key = '_sku' WHERE p.post_type = 'product' AND (pm.meta_value = '' OR pm.meta_value IS NULL) |
To remove the products, I think this should do. Of course this wouldn’t delete the files in your upload folder. If you want to delete those as well, you might want to write a script that selects the records, and then removes the images as well.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
DELETE wp_posts, wp_postmeta, wp_comments, wp_commentmeta FROM wp_posts LEFT JOIN wp_postmeta ON wp_postmeta.post_id = wp_posts.ID AND wp_postmeta.meta_key = '_sku' LEFT JOIN wp_comments ON wp_comments.comment_post_ID = wp_posts.ID LEFT JOIN wp_commentmeta ON wp_commentmeta.comment_id = wp_comments.comment_ID LEFT JOIN wp_postmeta wpm2 ON wpm2.post_id = wp_posts.ID WHERE wp_posts.post_type = 'product' AND (wp_postmeta.meta_value= '' OR wp_postmeta.meta_value IS NULL) |
This saved me so much time, thanks a million!
i have the same issue with wp all import plugin, and this help us! thanks!!!!
i need to delete more than10000 product.
how about duplicated SKU? how to remove that from woocommerce . the one i have hangs a long time.
I don’t have a running WooCommerce anymore. But if I were you I’d first query the suplicate SKU’s. See how many you get. You could then also create a delete query from that, using the id’s with some creative copy & pasting.
Pingback: Eliminar productos duplicados por SKU en Woocommerce - Kawaineko
Hello –
This could be my saviour! I have a strange problem with my database where it is adding around 30% additional product variations when I import the database (they all have unique IDs, but no SKU). Even when I delete everything and try again, the same thing happens.
I can use this to delete those products, but is there a way of getting rid of the product variations too?
I know there are around 2,500 variations that need to be deleted, but the result using the query below only brings up 200 products.
Any help would be very much appreciated!
Kind regards,
Ollie
Just to update, I replaced ‘product’ with ‘product_variation’ and it worked like a dream.
Thanks for posting this on your website. I have spent over a week trying to solve all sorts of database headaches, and this has saved me!
WP All Import has a setting, located in the template area, under WooCommerce Add-On > Import Options, that enables SKU duplicate checking.
If not checked, and duplicate SKUs appear, it will import them with an empty SKU.
Reading the above posts, it kind of clicked for me that this was the reason for this to happen.
If so, my question is, how to check and automate the deletion of all products with empty SKU just after imports finishes?
Thanks in advance for bringing this topic, would be great to find a way to really prevent that to happen in production!
I’m not sure, I am not a WooCommerce or WordPress developer. Maybe a plugin can developed if there is an WooCommerce after_import hook.
You should be able to put the SQL query in a crontab though, that would automate it on a specific time.