cancel
Showing results for 
Search instead for 
Did you mean: 

Delete all categories at once

Delete all categories at once

We have been experimenting with syncing our existing database(not Magento) to Magento 2.0.

 

In the process we have created over 1000 categories, but then we rearranged all the IDs to sync with our system. The problem is that I can no longer import the new categories as I get the error "URL key for specified store already exists." and we are getting duplicate categories now.

 

I think the best solution would be to clear them all out and import fresh. I found this tutorial for 1.9

https://gist.github.com/jklance/9664371

 

When I look at the new database I notice small differences

1. entity_type_id is in all the old categoriy tables but none of the new ones.

2. new database has an additional table catalog_category_product_index_tmp

 

My questions are these,

 

1. can I use that code I found and modify to remove "entity_type_id"
and add TRUNCATE TABLE ` catalog_category_product_index_tmp`;

 

2. or is there more I need to modify?

 

3. or can you provide me a clean script to remove all categories?

 

we only have 1 test product in the system, so no need to worry about them

1 REPLY

Re: Delete all categories at once

The same code would not work because the url_rewrite table does not have the same foreign keys

 

Assuming you are not fussed about resetting the category ids, the following should suffice;

 

DELETE FROM catalog_category_entity WHERE entity_id > 2;
DELETE FROM url_rewrite WHERE entity_type = 'category';

 

I have tested the above on a base 2.0.2 install by adding a new category and then running the above code.

 

Problem solved? Click Accept as Solution!
www.iwebsolutions.co.uk | Magento Small Business Partner