Windows OS

How to Delete Files or Folders Recursively on Windows

Folders and files are the main components of the operating system used to store data, software programs, and even windows programs. Sometimes users do not need some folders or files and wants to delete them. This deletion can be carried out using command line applications i.e., PowerShell and Command Prompt. Recursive folder deletion indicates that the user wants to remove the folder together including files, sub-directories, and data.

In this article, we will elaborate on several methods to delete folders or files recursively on windows. The content of this post is provided below:

  • Using Command Prompt
  • Using PowerShell

So, let’s start!

How to Delete Files or Folders Recursively on Windows Using CMD?

Command Prompt (CMD) offers a variety of commands to perform any operation on windows. The “rd” and “rmdir” commands can be used to delete files or folders recursively. Here, we will use the above-mentioned commands to perform the recursive deletion of files/folders.

Using rd Command

To remove files or folders on Windows recursively utilize the “rd” command. Here, the “/s” option will delete the folders and all files recursively, and the “/q” option will prevent asking for confirmation:

> rd /s /q "C:\Users\anuma\OneDrive\Desktop\React"

The above-mentioned command will delete the folder named “\React” placed at the location “C:\Users\anuma\OneDrive\Desktop\React“:

The successful execution of command indicates that the folder has been deleted successfully.

Using rmdir Command

We can use the “rmdir” command to delete folders or files in Command Prompt. Similarly, “/s” is used to remove subfolders and files recursively:

> rmdir /s /q "C:\Users\anuma\OneDrive\Desktop\C program"

To remove a folder by specifying the folder name, utilize the provided command. The “if” condition is applied to check whether the folder exists or not:

> if exist C program (rmdir /s/q C program)

How to Delete Files or Folders Recursively on Windows Using PowerShell?

The PowerShell is a Windows command line interface/shell used to manage and operate administrative tasks. In Windows PowerShell, the following commands are used to remove files or folders:

  • Using “Remove-Item” cmdlet
  • Using “rm” command
  • Using Delete() method

Let’s examine each of the above-mentioned commands.

Using Remove-Item Cmdlet

The “Remove-Item” command is primarily used to remove folders or files. Firstly, use the “cd” command to open the directory:

> cd E:

Next, utilize the “Remove-Item” command with the “-Recurse” option to delete files or folders recursively.

Use the “-Force” option for forceful deletion of the folder if it contains any hidden file:

> Remove-Item -Recurse -Force "Cprogram"

Using Get-ChildItem with Remove-Item cmdlet

The “Get-ChildItem” command is used to retrieve the file or folder from the specified path. Here, we have used the “Get-ChildItem” cmdlet to retrieve the content of the folder and then the content is piped with the “Remove-Item” cmdlet to remove the retrieved content.

> Get-ChildItem E:\Cprogram -Recurse -Filter "mydata" | Remove-Item -Force -Recurse

The above-given output indicates that we have successfully deleted the specified folder.

Using the rm Command

To remove files and folders, use the “rm” command. The “-r” option is used to delete the folder recursively, and the “-fo” option represents the “-Force“:

> rm -r -fo E:\Cprogram

Using the Delete() Method

Access the target folder using the “Get-ChildItem” command and then use the “Delete()” method to delete all of its files and folders inside that folder:

> (Get-ChildItem E:\Cprogram).Delete()

We have compiled the different commands of Command Prompt and PowerShell to delete folders or files recursively on Windows.

Conclusion

To delete files or folders recursively, you can utilize Windows Command Prompt or Windows PowerShell. In the Command Prompt, use “rmdir” or “rd” commands with the “/s” option is used to remove folders recursively. In PowerShell, the “Remove-Item” cmdlet, “rm” command, and “Delete()” method can be used to delete a folder or file recursively. We have demonstrated all the commands with the help of suitable examples to delete a folder or file recursively on windows.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.