How to Delete a Folder With Its Contents Using a Batch File in Windows

Siddharth Bishnoi Feb 02, 2024
  1. Delete a Folder With Its Contents Manually Through the Command Line
  2. Delete a Folder With Its Contents Using a Batch File
How to Delete a Folder With Its Contents Using a Batch File in Windows

Deleting folders along with their content can be easily done through Windows Explorer. But, if the files or folders are larger, it will be time-consuming; a folder larger than 5GB in size will take at least 5 to 10 minutes to completely erase.

The fastest way to delete files and folders is through the command line. We can either do it manually using the cmd or create a Batch file.

This tutorial will teach you how to delete folders and subfolders using a Batch file.

Delete a Folder With Its Contents Manually Through the Command Line

The rmdir command is used to delete a directory (folder), the same as the rd command. It removes the specified folder along with its subfolders and files.

The syntax of the rmdir command is shown below.

rmdir [/s [/q]] [<drive>:]<path>

Parameters:

  • [<drive>:]<path> - specifies the location of the folder.
  • /s - deletes the specified folder and its subfolders and files.
  • /q - specifies Quiet mode. It does not prompt for a confirmation when deleting a folder, and it will only work if /s is specified.

To delete a folder and its contents manually, open the command prompt through the Start menu or search bar. Copy the command as shown below.

The following code uses the rmdir command to delete a directory tree.

rmdir /s /q D:\TESTFOLDER\test

Delete Folders Using CMD

Change the destination path to the folder location that you want to delete. The folder will be deleted along with its content.

Delete a Folder With Its Contents Using a Batch File

Rather than deleting folders manually using the command line, we have a one-click solution by creating a Batch file to delete the folders automatically. After the Batch file is created, you can run the Batch file whenever you need to delete a folder.

A Batch file is used to execute commands in a serial order stored in a plain text file. It is saved with the .bat extension.

Here, we have a folder named test1 with subfolders and files. In this article, we will use this folder as an example.

Test Folder

Delete a Single Folder With Its Contents

To create a batch file, open Notepad from the Start menu and copy the commands as shown below:

echo Delete folders using a batch file
rmdir /s /q D:\TESTFOLDER\test1

Delete a Folder Using a Batch File

Save the file with a .bat extension.

Bat File

Now, run the file by double-clicking on it to delete a folder and its content. The above command will delete the specified directory along with its content.

We can also delete multiple folders using a Batch file by adding the paths of the different folders. Here, we have two folders named test1 and test2, which we will take as an example.

Test Folders

Delete Multiple Folders With Its Contents

Open Notepad and copy the commands shown below:

echo Delete folders using a batch file
rmdir /s /q D:\TESTFOLDER\test D:\TESTFOLDER\test1

Delete Multiple Folders

Save the file with a .bat extension, and run the .bat file by double-clicking.

So, we discussed how to delete a folder and its content manually using cmd and a Batch file. Using the Task Scheduler application, you can use Batch files to perform repetitive tasks, automate tasks, or even schedule tasks.