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:
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:
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:
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:
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:
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.
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“:
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:
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.