I am using the following code to delete a category from my Magento installation programmatically:

        $hostId = $model->getId()->getHost();
        $category = \Mage::getModel('catalog/category')
            ->loadByAttribute('jtl_erp_id', $hostId);

        if ($category) {
            \Mage::register('isSecureArea', true);

This works fine unless the flat category index is enabled. If this is the case, the following exception is being thrown:

Fatal error:  Call to undefined method Mage_Catalog_Model_Resource_Category_Flat::isForbiddenToDelete() in /path/to/magento/app/code/core/Mage/Catalog/Model/Category.php on line 755

Any ideas? I am using Magento

2 Answers 2


You are not supposed to delete categories outside the Admin scope.
You can just simulate the admin environment and it should work

$appEmulation = Mage::getSingleton('core/app_emulation');
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation(Mage_Core_Model_App::ADMIN_STORE_ID);


// end admin store emulation

I faced the same issue when flat_categories were enabled.

You can load the normal category model instad of the flat category and delete it to get around this issue

Mage::register('isSecureArea', true);
$category = Mage::getModel('catalog/category', array('disable_flat' => true))->load($categoryId);

Referance: disable flat category and load

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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