Remove magento categories

The following function() delete all the categories from magento 1 programmatically. Please click here to refer the full code which removes all the product and categories.

Please click here to find how to delete all the categories in magento 2 programmatically.The tutorial also give step by step explanation.

function removeAllCategories(){
    $resource = Mage::getSingleton('core/resource');
    $db_read = $resource->getConnection('core_read');
    $categories = $db_read->fetchCol("SELECT entity_id FROM " . $resource->getTableName("catalog_category_entity") . " WHERE entity_id>2 ORDER BY entity_id DESC");
    foreach ($categories as $category_id) {
        try {
            Mage::getModel("catalog/category")->load($category_id)->delete();
            echo 'Category Removed '.$category_id .PHP_EOL;
        } catch (Exception $e) {
            echo 'Failed to Remove product '.$category_id .PHP_EOL;
            echo $e->getMessage() . "\n" .PHP_EOL;
        }
    }
}

Share this Tutorial