<?php 
error_reporting(E_ALL);
ini_set('display_errors', '1');
 
// Load Up Magento Core
define('MAGENTO', realpath(''));

require_once(MAGENTO . '/app/Mage.php');
 
$app = Mage::app();
 
$categoryCollection = Mage::getModel('catalog/category')->getCollection()
    ->addFieldToFilter('level', array('gteq' => 2))
;

foreach($categoryCollection as $category) {
    if ($category->getProductCount() === 0) {
        $category->delete();
    }
}
?>