I have tried numerous methods of doing this including Remove-Item, rmdir, del, and rd. I have seen similar questions, but none of the answers have helped me. To be clear, I am not asking for a specific "book, tool, or software library"; I just want a Batchfile command to recursively delete a folder. I'm sorry if I'm doing something wrong; I'm pretty new here.
How to delete a non-empty folder in Batch script?
6.8k views Asked by UxuginLinux At
2
There are 2 answers
6
On
To delete a non empty folder, you need something called recursion. What we usually read is that we want to "delete a folder recursively", which means to delete a folder and all its contents, including other folder, with their respective contents and folders, and so on.
The command del does it for you. Have you read its help or its documentation? To see that, the command you use in the cmd program is:
del /?
But this does not directly answer your question. What you need to put in your batch file is:
del /s /q [non empty folder name]
- "/s" is to delete it recursively
- "/q" is to delete it without asking for confirmation of each file or folder being deleted. You may want to remove this item, if you want to choose what will be deleted inside the folder.
- You must not write the square brackets I wrote in the command example. Just the folder name, as it is.
For the previous answer, I checked the current Microsoft documentation about commands it has, and it does NOT say clearly that the del command will delete only files.
The command you need to recursively delete a folder, and all files OR folders it contains is:
Please note the "/s" and "/q" arguments, which have the same meaning as for the del command, but they come AFTER the name of the folder! This is what the command documentation shows, as you may read here.
But there are more possible reasons for the recursive directory deletion failing! If you try to delete a directory that has system files or hidden files, the rmdir command will fail. To solve this problem, you need to do more work. To quote the documentation pointed above: