DELTREE - Delete all subfolders and files.

Very old versions of Windows (Windows 95 and earlier) had the DELTREE command to delete all sub-folders and files. Newer versions of Windows do not have this command, but we can easily write a short batch script to do the same thing.

Deleting from the command line is significantly faster than using Windows Explorer, often seconds instead of minutes, there is no running calculation of file sizes and no recycle bin. This does mean there is no possibility of an undo other than restoring a backup.

The two key commands required are DEL /s to delete all files including hidden and system files, followed by RD /s to remove the now empty folders.

When iterating through thousands of files, supressing the output of DEL *.* by redirecting it to NUL, will make the process run a little faster.

:: DelTree.cmd
:: Delete a folder plus all files and subfolders
@Echo Off
Set _folder=%1
if [%_folder%]==[] goto:eof

PUSHD %_folder%
:: abort if this fails
If %errorlevel% NEQ 0 goto:eof

Del /f /q /s *.* >NUL
CD \
RD /s /q %_folder%
:: repeat because RD is sometimes buggy 
if exist %_folder% RD /s /q %_folder%


Supply the full path to the folder to be deleted surrounded in quotes:
deltree.cmd "c:\demo\sample files"

Delete only Empty folders

Delete all folders and subfolders below the current folder.
Copy the script below to a folder and simply double click it.

:: DelEmpty.cmd
:: Remove all empty folders and subfolders
@Echo off
Set _folder="%~dp0"

PUSHD %_folder%
:: abort if this fails
If %errorlevel% NEQ 0 goto:eof

Echo remove empty folders from %_folder% ?
For /f "delims=" %%d in ('dir /s /b /ad %_folder% ^| sort /r') do RD "%%d" 2>nul

Alternative PowerShell one-liner to delete empty folders:
PS C:\> Get-ChildItem -Recurse . | where { $_.PSISContainer -and @( $_ | Get-ChildItem ).Count -eq 0 } | Remove-Item

Delete the Current Folder & subfolders

This script deletes all contents of the current folder. Copy the script to a folder and simply double click it.
This version also does not delete the root folder itself. The script sets the read-only Attribute on itself.

:: FastDel.cmd
:: Remove all files and subfolders
@Echo Off
Set _folder="%~dp0"
Attrib +R %0

PUSHD %_folder%
:: abort if this fails If %errorlevel% NEQ 0 goto:eof
ECHO Delete all contents of the folder: %_folder% ? Pause :: Delete the files Del /f /q /s /a:-R %_folder% >NUL :: Delete the folders For /d %%G in (%_folder%\*) do RD /s /q "%%G" Attrib -R %0 Popd

If you use this on a UNC path like \\Server64\share1\somefolder the CMD shell will not be able to set a current directory. To avoid an automatic fallback to C:\Windows\ the script uses %~dp0 to grab the location and then pushd will, if needed, map a temporary drive.

“However beautiful the strategy, you should occasionally look at the results” ~ Sir Winston Churchill


RD - Delete folders or entire folder trees.
DELPROF Delete NT user profiles.
DEL - Delete files.
PowerShell: Remove-Item - Delete the specified items.
Equivalent bash command (Linux): rmdir / rm - Remove folders/ files.

Copyright © 1999-2019
Some rights reserved