I have tried to disable categories in Magento 2

I have tried to achieve this via PHP script like below


use \Magento\Framework\App\Bootstrap;

require __DIR__ . '/../app/bootstrap.php';

$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);

$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('\Magento\Framework\App\State');

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$categories = $categoryFactory->create()                              
    ->addAttributeToSelect('*'); //categories from current store will be fetched
echo count($categories);
foreach ($categories as $category){
    $category_id = $category->getId();
    echo $category_id."<br>";

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $collection = $objectManager ->get('Magento\Catalog\Model\Category')->load($category_id);
    $collection->setData('is_active', 0);
    echo "success";

But I got following error

Fatal error: Uncaught Magento\Framework\Exception\LocalizedException: Invalid URL key in /var/www/html/vkm/vendor/magento/module-catalog-url-rewrite/Observer/CategoryUrlPathAutogeneratorObserver.php:93

Please provide a solution to disable categories in Magento 2

1 Answer 1


Hi I have tried to edit directly in database like below and I have achieved what I want based on Disable All Children of Disabled Category for Store View

UPDATE catalog_category_entity_int SET value = '0' WHERE entity_id IN ({{add category id's by comma separate }}) AND attribute_id=(SELECT attribute_id FROM eav_attribute WHERE attribute_code = "is_active");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.