I know there are so many questions already asked related to this issue. But mine one is little difference. I created one custom module for import the products in Magento 2. In which categories will be created automatically if not exists. Its working fine. When there is no category in store but when there is any category in store and I try to define it in csv file then I am getting "URL key for specified store already exists." error. I already checked so many answers and tried so many things but not get proper success.
AFAIK, You may need to delete the URL Rewrites:
MARKETING > SEO & Search > URL Rewrites
Just like every product, every category has a unique URL key too and you can’t have two products or categories with same URL key.
You can access the URL key of any category by selecting the category from your admin panel.
So when you try to add a category through CSV that already exists you’ll get the error "URL key for specified store already exists."
Make sure new categories have a unique name and a URL key.
After looking on internet for days I couldn't find exact solution of this. Then I found if we change the URLKEY of the category it will not show this error so I have tried this.
$category->setPath($parentCategory->getPath()) ->setParentId($parentId) ->setName('test1') ->setIsActive(true) ->setUrlKey(rand(1,1000000000)); $category->save();
I have used random function to add category in database with random nunmber using
->setUrlKey(rand(1,1000000000)); you can add any thing in this like duplicate category name with some random number etc.
and errors gone if it helps you give an UP. thanks