---
title: "How to Programmatically Delete Directory in Magento 2?"
url: "https://magecomp.com/blog/programmatically-delete-directory-magento-2/"
date: "2021-12-13T15:53:12+00:00"
modified: "2025-02-20T06:21:10+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 245
reading_time: "2 min read"
summary: "Hello Magento Friends,"
description: "When you have created any folder while developing but later you want to delete it. Learn How to Programmatically Delete Directory in Magento 2"
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Magento 2: How to Get Parent Product ID from Child Product ID for Bundled and Grouped Products"
    url: "https://magecomp.com/blog/magento-2-get-parent-product-id-from-child-product-id-for-bundled-and-grouped-products/"
  - title: "Solved: “TypeError: opConfig is undefined” Error After Magento Upgrade"
    url: "https://magecomp.com/blog/solved-error-magento-upgrade/"
  - title: "How to Change Length of Order Increment ID in Magento 2?"
    url: "https://magecomp.com/blog/change-length-of-order-increment-id-magento-2/"
---

# How to Programmatically Delete Directory in Magento 2?

_Published: December 13, 2021_  
_Author: Dhiren Vasoya_  

![How to Programmatically Delete Directory in Magento 2](https://magecomp.com/blog/wp-content/uploads/2021/12/How-to-Programmatically-Delete-Directory-in-Magento-2-1024x512.webp)

**Hello Magento Friends,**

Today I am going to explain How to Programmatically Delete Directory in Magento 2?
Magento 2 contains various directories and subdirectories which have files in them. The directory structure of Magento 2 helps developers to [**develop custom extensions**](https://magecomp.com/magento-custom-extension-development.html), themes or add any custom code.
[![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-1.webp)](https://magecomp.com/services/magento-upgrade-services/)

If you want to delete any unnecessary directory in Magento 2 you can do it programmatically and avoid deleting it manually, I have explained it below
## Steps to Programmatically Delete Directory in Magento 2:
**Step 1:** Go to the below path
**app\code\Vendor\Extension\etc\di.xml**

And add the below code
```
<?php
namespace VendorExtensionControllerAdminhtmlDeleteimages;

use MagentoFrameworkAppFilesystemDirectoryList;
use MagentoFrameworkFilesystem;

class Index extends MagentoBackendAppAction
{
    protected $fileSystem;
    protected $resultPageFactory;
    protected $_publicActions = ['index'];

    public function __construct(
        MagentoBackendAppActionContext $context,
        MagentoFrameworkViewResultPageFactory $resultPageFactory,
        Filesystem $fileSystem)
    {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
        $this->directory = $fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
    }

    public function execute()
    {
        $resultPage = $this->resultPageFactory->create();

            $dir = '/test/';
            $deleteDirectory = $this->directory->delete($dir);
            $this->messageManager->addSuccess(__('Custom Images Deleted Successfuly!'));

        return $resultPage;
    }
}
```

[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-1.webp)](https://magecomp.com/magento-performance-optimization.html)

So this blog will help to remove the folder test from the pub/media folder.

## Conclusion:
This way simply you can Programmatically Delete Directory in Magento 2. If you have any doubts, let me know via the comment section below. Share the article with your friends and stay in the know for further tutorials.
[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-3.webp)](https://magecomp.com/services/hyva-theme-development/)

**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/programmatically-delete-directory-magento-2/](https://magecomp.com/blog/programmatically-delete-directory-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-21 10:12:34 UTC_  
