---
title: "Magento 2: How to Delete Categories using Root Script by Category IDs"
url: "https://magecomp.com/blog/magento-2-delete-categories-using-root-script-by-category-ids/"
date: "2025-03-17T07:11:19+00:00"
modified: "2025-03-17T07:14:12+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 327
reading_time: "2 min read"
summary: "Hello Magento Friends,"
description: "Learn how to delete categories in Magento 2 using a root script by category IDs."
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Magento 2: How to Create Custom Product Slider in Hyvä Themes"
    url: "https://magecomp.com/blog/magento-2-create-custom-product-slider-hyva-themes/"
  - title: "How to Override “payment\methods.phtml” in Magento Custom Extension"
    url: "https://magecomp.com/blog/override-paymentmethods-phtml-in-magento-custom-extension/"
  - title: "How to Use Built-In jQuery Library in Magento 2"
    url: "https://magecomp.com/blog/how-to-use-built-in-jquery-library-in-magento-2/"
---

# Magento 2: How to Delete Categories using Root Script by Category IDs

_Published: March 17, 2025_  
_Author: Dhiren Vasoya_  

![Magento 2 How to Delete Categories using Root Script by Category IDs](https://magecomp.com/blog/wp-content/uploads/2025/03/Magento-2-How-to-Delete-Categories-using-Root-Script-by-Category-IDs-1024x512.webp)

**Hello Magento Friends,**

Magento 2 offers a robust backend for product category management, but there are instances where you need to delete categories programmatically from a root script. This is especially helpful in the case of bulk category deletions, migration cleanup, or automation.

[![Hire Magento Programmer](https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-1.webp)](https://magecomp.com/services/hire-magento-programmer/)Here in this blog, I will describe how to delete categories from a root script in Magento 2 using category IDs.

## Steps to Delete Categories using Root Script by Category IDs in Magento 2:
**Step 1:** Create a file in your Magento root directory at the following path

**magento_root_directory\deletecategory.php**

Then insert the code as follows

```
<?php
use MagentoFrameworkAppBootstrap;
use MagentoFrameworkAppState;
use MagentoFrameworkRegistry;
use MagentoCatalogModelCategoryFactory;

require __DIR__ . '/../app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

try {
    // Set the area code to 'adminhtml'
    $state = $objectManager->get(State::class);
    try {
        $state->setAreaCode('adminhtml');
    } catch (MagentoFrameworkExceptionLocalizedException $e) {
        // Area code already set, continue
    }

    // Define the category IDs to delete
    $categoryIds = [3,4,5]; // Replace with your actual category IDs

    // Load Registry
    $registry = $objectManager->get(Registry::class);

    foreach ($categoryIds as $categoryId) {
        try {
            // Load and delete the category
            $categoryFactory = $objectManager->get(CategoryFactory::class);
            $category = $categoryFactory->create()->load($categoryId);

            if ($category->getId()) {
                // Allow delete operation in non-secure area
                $registry->register('isSecureArea', true);

                $category->delete(); // Delete the category

                // Unregister secure area
                $registry->unregister('isSecureArea');

                echo "This $categoryId Category ID is deleted successfully.";
            } else {
                echo "This $categoryId Category with ID Not deleted";
            }
        } catch (Exception $e) {
            echo "Something wrong went delete category: " . $e->getMessage();
        }
    }
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}
```

**Step 2:** Now, after the above step, you must run the below-given URL.

<https://yourdomain.com/deletecategory.php>

**Output:**

**Before Deleting Categories:**

![before deleting categories](https://magecomp.com/blog/wp-content/uploads/2025/03/before-deleting-categories-1024x423.webp)**After Deleting Categories:**

![after deleting categories](https://magecomp.com/blog/wp-content/uploads/2025/03/after-deleting-categories-1024x420.webp)

## Conclusion:
With a basic root script, you can quickly remove several categories in Magento 2 by category IDs. This is time-saving and effort-saving, particularly when dealing with large catalogs.

In case you have some questions or need help, please leave a comment!

[![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-2-2.webp)](https://magecomp.com/services/magento-upgrade-services/)**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/magento-2-delete-categories-using-root-script-by-category-ids/](https://magecomp.com/blog/magento-2-delete-categories-using-root-script-by-category-ids/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-09-24 16:21:10 UTC_  
