Programmatically delete all Magento categories

You’re developing a Magento importer and you want to clean your DB after some tests? Here you have:

require_once dirname(__FILE__) . '/app/Mage.php';

$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>1 ORDER BY entity_id DESC");
foreach ($categories as $category_id) {
	try {
	} catch (Exception $e) {
		echo $e->getMessage() . "\n";

3 thoughts on “Programmatically delete all Magento categories

  1. Nilo

    Just to let you now that you should not delete default category entities, at least not the Default Category which has id=2 on catalog_category_entity. This breaks the UI on catalog admin.

  2. eyemaginesrobbins

    ->addFieldToFilter('entity_id', array('gt' => 2)) as $cat) {

Leave a Reply

Your email address will not be published. Required fields are marked *