Skip to content

Instantly share code, notes, and snippets.

@nobesnickr
Last active August 14, 2021 03:34
Show Gist options
  • Save nobesnickr/b2f0af33f2ea8ceaa21cffaa1086e0e9 to your computer and use it in GitHub Desktop.
Save nobesnickr/b2f0af33f2ea8ceaa21cffaa1086e0e9 to your computer and use it in GitHub Desktop.
Windows Batch Script to Delete Ridiculous Amount Of Files

Script Details

Key Value
Author Phil McCaffrey [email protected]
Created Date Wed Aug 11th, 2021 @ 17:50:24
OS Compatibility Tested on
  • Windows 10
  • Sever 2012/2016 |

For a given path (a.k.a. Folder)... quickly, recursively and permanently delete all files and folders within it.

Purpose / Background

In Windows, deleting a tremendous amount of files (i.e. 2 million files or 1.5 tb worth of files) can be done in a number of ways. Unfortunately, most of the easy/available ways that the "average Windows User"* would attempt it will take a ridiculously long time and may have multiple failures or problems.

In the "Command Line" there are a few fast and reliable options available to those who are both aware of them as well as familiar and comfortable using a cli. Given the inherent destructive nature of deletion, combined with the lack of safety net in the CLI, is a recipe for disaster.

  • "average Windows User" is respectfully defined as those who dont know or are not comfortable with the CLI (Command Prompt, PowerShell, etc.)

TLDR: The purpose of this script is to provide safe, convenient, dead-simple access to the best and fastest tools Windows offers for deleting a tremendous amount of files.

Usage Instructions

It would be very wise to fully read and understand this entire page before doing anything.

  1. Download this script onto Windows computer/server containing files/folders needing to be deleted
  2. Open "File Explorer" and navigate to where the script has been downloaded
  3. Run the script by right clicking it and going to "Run as Administrator"
  4. A new "Command Prompt" will launch and details of the system executing the script will be shown
  5. The purpose of showing the system details is to allow the user to confirm script is being executed on the expected/desired system. Absolutely no data is extracted, logged, or transmitted anywhere in this script
  6. When prompted, Type or "Paste" the full path of the folder to be deleted (i.e. c:\sandbox\someFiles), then press enter
  7. A prompt will be shown to confirm the path/folder to be deleted.
  8. If the prompted path is EXACTLY as expected/desired, press 'Y'... otherwise press 'N'

Warnings

  • This script provides no ability to "selectively" delete (or not-delete) within the given "path". EVERYTHING in and "under" the path/folder specificed will be irrevocably deleted. There is no "Recycle Bin" or "Undo" possibility once the deletion process is confirmed then executed
  • The path shown in the confirmation prompt (Step 7 above) is the "scripts" understanding of the path input by the user, and the path that will in fact be deleted. Usually what the user enters and what the "script understands" will be the same thing, but there are certain situations that may not be the case.

Disclaimer

  1. This script is provided at no cost, as my gift to you/humanity. FWIW, I wish it was a nicer gift too...
  2. Although I have every reason to believe it will work as expected, I take no responsibility for using it or any possible outcome of doing so. In fact, if we're getting all legal about it, my advice is not to use this for any reason under any circumstance. 2.1. If needed, I will happily provide a full money-back guarantee... which is of course nothing. 2.2. All refunds will be made in Wompum, Monopoly Money, or other as decided by me
  3. Where required by law, and with irrefutable proof, I accept liability of $0.0000000000000000001 3.1. To be accepted, proof must be accomponied by a picture containing at least 3 chickens, a unicorn, a mime, and the original output of the script rendered through a monitor which was made before 1953
  4. Be awesome and make the world a better place
:: Created by Phil McCaffrey <[email protected]>
:: For updates, usage instructions and more... @see https://gist.github.com/nobesnickr/b2f0af33f2ea8ceaa21cffaa1086e0e9
@echo off
cls
TITLE Windows Massive Files Deleter
setlocal EnableExtensions EnableDelayedExpansion
:: Establish Variables
set _performDelete="false"
set _performRobocopyDelete="false"
set _emptyFolderProtoPath=%temp%\emptyFolderPrototype
set _rootPath=""
:: LocalDateTime = ldt
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set ldt=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2% %ldt:~8,2%:%ldt:~10,2%:%ldt:~12,6%
echo Windows Tremendous Files Deleter
echo ======================================
echo.
IF %_performDelete% NEQ "false" (
echo Protection Values Check 1 Failed... exiting...
goto :END
)
IF %_performRobocopyDelete% NEQ "false" (
echo Protection Values Check 2 Failed... exiting...
goto :END
)
goto :displaySystemInformation
:displaySystemInformation
echo Gathering system info... please wait...
echo.
echo.============================
echo SYSTEM INFO
echo ============================
systeminfo | findstr /c:"OS Name"
systeminfo | findstr /c:"OS Configuration"
systeminfo | findstr /c:"Host Name"
echo Local Date/Time: %ldt%
set ip_address_string="IPv4 Address"
for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:%ip_address_string%`) do echo IPv4 Address: %%f
echo Temp Folder: %temp%
where robocopy >nul 2>&1 && (
echo robocopy AVAIL: SUCCESS
) || (
echo robocopy AVAIL: FAILED!!!
)
echo.
echo ============================
echo DISK/DRIVE INFO
echo ============================
wmic logicaldisk get caption,providername,drivetype,volumename,size
goto :confirmIsCorrectSystem
:confirmIsCorrectSystem
%SystemRoot%\System32\choice.exe /C YN /N /M "Proceed using this system? [Y/N]"
if not errorlevel 2 if errorlevel 1 goto setupRootPath
goto END
:setupRootPath
if "%~1" == "" (
echo No path argument supplied upon execution... enter full path to use below
set /p _rootPath="Root Directory Path: "
) else (
set _rootPath="%~1"
)
if %_rootPath% == "" goto :emptyInputPath
if not exist %_rootPath%\ goto :invalidInputPath else goto :confirmDeleteChoice
:confirmDeleteChoice
%SystemRoot%\System32\choice.exe /C YN /N /M "Are you sure want to delete %_rootPath% [Y/N]?"
if not errorlevel 2 (
if errorlevel 1 (
set _performDelete="true"
goto performPrimaryDeleteStrategy
)
) else (
goto declinedDeletion
)
:invalidInputPath
echo %_rootPath% is not valid or does not exists on this machine... exiting...
goto END
:emptyInputPath
echo Path value was not supplied at script execution or user input ... exiting...
goto END
:declinedDeletion
echo User decline recieved, exiting without performing any actions...
goto END
:performPrimaryDeleteStrategy
IF %_performDelete% NEQ "true" (
echo Invalid Access Vector Detected... exiting...
goto :END
)
echo Confirmation recieved, proceeding with deletion...
echo STARTED AT: %date% %time%
echo.
echo Phase 1 of 2 [Delete all files] starting at %date% %time%
del /f/s/q "%_rootPath%" > null
:: if del fails for any reason, switch to using robocopyNuclearOption
IF %ERRORLEVEL% NEQ 0 (
set _performRobocopyDelete="true"
goto robocopyNuclearOption
) else (
echo Phase 1 completed successfully at %date% %time%
)
echo Phase 2 of 2 [Deleteing folders] starting at %date% %time%
rmdir /s/q "%_rootPath%"
IF %ERRORLEVEL% NEQ 0 goto robocopyNuclearOption else echo Phase 2 completed successfully at %date% %time%
echo.
echo Deletion Phases have been completed.
goto END
:robocopyNuclearOption
:: If the regular "del" fails, with filenames that are too long for example, defer to using robocopy purge
IF %_performRobocopyDelete% NEQ "true" (
echo Invalid Access Vector Detected... exiting...
goto :END
)
echo Deletion failure detected, falling back to using robocopy purge instead
echo.
echo Creating empty temp directory at: %_emptyFolderProtoPath%
mkdir %_emptyFolderProtoPath%
echo Executing robocopy purge of %_rootPath% at %date% %time%
robocopy %_emptyFolderProtoPath% %_rootPath% /purge
echo Finished robocopy purge at %date% %time%
goto END
:END
endlocal
echo.End of script reached
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment