• Resolved craigs85

    (@craigs85)


    Hi there,
    Since deleting literally thousands of products, I have a lot of categories that have a 0 product count.

    I could sort by the count in the category list page and delete manually this way, but if I do more than 10 at a time, it ends up timing out as there are so many categories in my store. (5500+).

    Is there a way that I can delete all of the categories that have 0 products in them via SQL?

    This would be much quicker and easier and once I’ve tidied up even more, I can re-use it.

    Many thanks in advance.
    Craig

Viewing 4 replies - 1 through 4 (of 4 total)
  • Remember to backup your database before you do any sql modifications.

    I have tested with my own woocommerce site, to remove categories with no products in it.

    DELETE a,c FROM wp_terms AS a LEFT JOIN wp_term_taxonomy AS c ON a.term_id = c.term_id LEFT JOIN wp_term_relationships AS b ON b.term_taxonomy_id = c.term_taxonomy_id WHERE c.taxonomy = 'product_cat' AND c.COUNT = 0;

    Thread Starter craigs85

    (@craigs85)

    Fab. I’ll give that a try later tonight after I’ve backed up of course.

    If I change delete to select, I assume this will still work and just show me what will be deleted by the query?

    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    Careful with this now though, you don’t want to delete the default category.

    Thread Starter craigs85

    (@craigs85)

    I gave it a try last night (after backing up of course), and it seemed to do the trick.

    It delete the number of categories that I expected it to.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Deleting Categories via SQL?’ is closed to new replies.