There are times when you may need to delete a non-empty directory (folder) from the command line in Windows. For example, you may want to clean up old project folders that still contain files and subfolders.
The regular delete commands like DEL and RMDIR will not work on non-empty directories. So how can you delete these folders from the command line?
In this article, we will explain two easy methods to remove non-empty directories in Windows using the Command Prompt:
Method 1: Using RD /S /Q
The RD (Remove Directory) command has two switches that allow deleting non-empty directories:
/S – Deletes the specified directory and all its subdirectories including any files.
/Q – Quiet mode, does not prompt for confirmation before deleting directories.
Here is the basic syntax:
RD /S /Q
For example, to delete the ‘C:\OldProject’ folder including all its files and subfolders, you would run:
RD /S /Q C:\OldProject
This will recursively remove everything under ‘C:\OldProject’ without asking for confirmation.
Method 2: Using DEL /S /Q
The DEL (Delete) command also has /S and /Q switches similar to RD:
/S – Deletes specified files from the given directory and all its subdirectories.
/Q – Quiet mode, does not prompt for confirmation before deleting files.
The syntax is:
DEL /S /Q
For example, to delete ‘C:\OldFolder’ and all its contents:
DEL /S /Q C:\OldFolder
This will silently remove all files and subfolders under the specified path.
When to Use RD /S /Q vs DEL /S /Q
Both RD and DEL can delete a non-empty directory tree when used with the /S and /Q switches. However, there is a slight difference:
– RD removes the target folder itself along with all contents.
– DEL removes all contents but does not delete the folder itself.
So if you need to delete both the folder and its contents, use RD /S /Q.
If you just need to empty the folder but keep the folder itself, use DEL /S /Q.
Be Careful Before Deleting!
These delete commands are very powerful and can cause permanent data loss if you accidentally run them against the wrong directory.
Here are some tips to be careful:
– Always run them first against a test folder to verify.
– Double check the path before hitting enter.
– Start the path with the target drive like C:\ to avoid accidentally deleting the wrong location.
– Consider using the /Q switch to prevent accidental data loss from confirmation prompts.
– If unsure, first backup the target folder before deleting just in case.
Other Delete Commands
Here are some other delete commands in the Windows Command Prompt and PowerShell:
Basic delete file command. But cannot remove folders.
DEL /F /S /Q
Force delete read-only files, recursively delete in subfolders.
DEL /F /S /Q
DEL /F /S /Q C:\Temp
Removes empty directories. Cannot delete non-empty folders.
Recursively removes empty subdirectories but not files.
RMDIR /S C:\TestFolder
Same as DEL command but supports wildcards.
PowerShell’s delete command. Supports files and folders.
Remove-Item -Recurse C:\TestFolder
As you can see, the Command Prompt offers several ways to delete files and folders. Use the above commands carefully and always double check what you are deleting.
How to Remove System Protected Folders
The methods above will not work on deleting special Windows system folders like Program Files, Windows, Users etc.
These folders are protected by the System File Protection feature. To delete them, you first need to disable the protection.
Here are the steps:
1. Open Command Prompt as Administrator.
2. Run following command:
takeown /f “C:\Program Files” /r /d y
3. This grants you ownership on the Program Files folder. Adjust the path as needed.
4. Next run:
icacls “C:\Program Files” /grant administrators:F /T
5. This gives Full access rights on Program Files.
6. Now you can run RD /S /Q to delete it.
7. Don’t forget to re-enable System File Protection after deleting protected folders!
How to Delete Folder With Long Paths
If the folder path is very long (over 255 characters), the above delete commands may fail with errors like:
“File name too long”
“Cannot find path” etc.
To delete very long path folders:
1. Open Command Prompt as Administrator
2. Run following command:
rmdir /S /Q “\\\?\
The \\?\ prefix tells Windows to process the long path correctly.
rmdir /S /Q “\\\?\C:\users\public\documents\long folder name…”
This will remove the folder with a long path.
How to Clear Disk Space After Deletion
When you delete large folders, the free disk space is not reclaimed immediately. To clear and refresh the free space, run:
1. Open Command Prompt
chkdsk C: /f
Replace C: with the drive letter you want to check.
This scans the drive and frees up space from deleted files.
You can also use the Disk Cleanup utility to purge deleted file space.
Deleting non-empty directories can be very handy for cleanup tasks. Just be careful to target only the intended folders for deletion to prevent mishaps.
The key commands are RD /S /Q and DEL /S /Q to force delete folder trees from the Windows command line.
Use additional switches like /F or \\?\ prefix when needed to overcome access denied errors on protected system paths or excessively long paths.
And don’t forget to reclaim disk space after massive deletions using CHKDSK or Disk Cleanup.