How to Delete everything in a directory suppressing errors
Recently in working with a client who was trying to delete old profiles from a directory, they were running into problems with certain folders in the directory. ย After it taking forever to even start the delete process in Windows, when an error would be encountered, the process would stop. ย This can lead to a very tedious and frustrating process of watching the delete action take place and then trying to catch it when it fails to start it all over again. ย However, this is the Windows GUI so one can only expect issues. ย Is there a command line way to do this which is much less painful? ย Absolutely!
By using the rmdir command, we can kick off the delete process using switches which will surpress any confirmations or errors from killing the process and also delete subdirectories that are not empty which is another problem one will encounter if trying to use only theย delย command to delete the files. ย So here is the command in full:
rmdir /s /q c:\yourdirectory
Note above, the “c:\yourdirectory” is any directory that you want to purge. ย Take a look at the screenshot below of the command syntax for what the switches mean and will do:
One thing to be caution of about this command though is that it also deletes the parent directory. ย So if there is something that is very critical about the directory itself, you might need to do something else for the delete. ย Otherwise this is a great command to purge a directory and suppress errors that would otherwise keep the process from completing successfully.