Wednesday , November 14 2018
Home >> All tutorial >> Magento delete empty categories and sub-categories

Magento delete empty categories and sub-categories

Remove all empty categories and sub-categories in Magento. When there are empty categories, the website shows empty page in those categories in frontend. Create a file in the magento root, I will name it rmvEmptyCats.php, with following code:

  1. require “app/Mage.php”;
  2. umask(0);
  3. Mage::app();
  5. $categoryCollection = Mage::getModel(‘catalog/category’)->getCollection()
  6.     ->addFieldToFilter(‘level’, array(‘gteq’ => 2)); //greater than root category id
  8. foreach($categoryCollection as $category) {
  9.     if ($category->getProductCount() === 0) {
  10.         $category->delete();
  11.     }
  12. }
  14. echo ‘Empty Categories Deleted!’;

Now you can easily run it by navigating to and wait for the message Empty Categories Deleted!

Note that this is going to DELETE those categories with zero product count.



Here is the library of Magento Download for FREE (all versions)

Your Magento website needs a powerful Magento ERP for stock control.


magento erp

About Champ

Leave a Reply

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

Heap | Mobile and Web Analytics