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.

In many cases, simply running RD from the command line will be orders of magnitude faster than using Windows Explorer. However for very large deletes (roughly 100,000 files plus) an additional significant time saving can be achieved by using a two pass approach: first deleting the files and then the folders.

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.

WARNING: if you have any directory junctions DEL will delete the destination directory and not the junction. Be very careful not to run these commands unless you know there are no junctions inside the target directory.

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%
::  If this fails, exit, we dont want to delete from the wrong folder.
If %errorlevel% NEQ 0 goto:eof

Del /f /q /s *.* >NUL
CD \
RD /s /q %_folder%
:: Repeat because RD is sometimes buggy and leaves a few files
if exist %_folder% RD /s /q %_folder%
Popd

Examples

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

In PowerShell this can be done as a one-liner:
Remove-Item -LiteralPath 'c:\demo\some folder to delete' -Force -Recurse

Delete only Empty folders

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

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

PUSHD %_folder%
:: If this fails, exit, we dont want to delete from the wrong folder.
If %errorlevel% NEQ 0 goto:eof

Echo remove empty folders from %_folder% ?
pause
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 double click it.
This version also does not delete the root folder itself. To avoid deleting itself, the script sets the read-only Attribute.

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

PUSHD %_folder%
:: If this fails, exit, we dont want to delete from the wrong folder. 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

Related commands

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-2024 SS64.com
Some rights reserved