This Gist shares how to install IObit Unlocker using Chocolatey and use a CMD batch script to force delete stubborn files or folders on Windows, including a verification step. This was tested on a minimal Windows 10 setup (Tiny10) on April 14, 2025.
-
Ensure Chocolatey is installed:
choco --version
If not installed, run:
powershell -Command "Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"
-
Install IObit Unlocker:
choco install io-unlocker -y
- This installs IObit Unlocker to
C:\Program Files (x86)\IObit\IObit Unlocker\
. - IObit Unlocker can unlock and delete files/folders that are in use or restricted.
- This installs IObit Unlocker to
The script takes a file or folder path as an argument, uses IObit Unlocker to force delete it, and checks if the deletion was successful. If IObit Unlocker fails, it falls back to native CMD commands.
Save this as ForceDelete.bat
:
@echo off
setlocal EnableDelayedExpansion
:: Check if a path was provided as an argument
if "%~1"=="" (
echo Usage: %0 "path\to\file_or_folder"
echo Example: %0 "C:\Users\John\Documents\stubborn.txt"
exit /b 1
)
:: Convert relative path to absolute path
set "target=%~f1"
:: Verify the path exists
if not exist "!target!" (
echo Error: The path "!target!" does not exist.
exit /b 1
)
:: Use IObit Unlocker to force delete the file or folder
echo Attempting to delete "!target!" using IObit Unlocker...
"C:\Program Files (x86)\IObit\IObit Unlocker\IObitUnlocker.exe" /delete "!target!"
set "unlocker_exit_code=!errorlevel!"
:: Check if the file/folder still exists after IObit Unlocker
if exist "!target!" (
echo IObit Unlocker failed to delete "!target!" ^(Exit Code: !unlocker_exit_code!^). Trying native CMD commands...
:: Attempt deletion with native CMD commands
if exist "!target!\*" (
:: It's a directory
rd /s /q "!target!" 2>nul
) else (
:: It's a file
del /f /q "!target!" 2>nul
)
:: Final check
if exist "!target!" (
echo Error: Failed to delete "!target!". It still exists.
echo Possible reasons: File/folder in use, insufficient permissions, or compatibility issues.
exit /b 1
) else (
echo Success: "!target!" has been deleted using native CMD commands.
)
) else (
echo Success: "!target!" has been deleted using IObit Unlocker.
)
exit /b 0
- Argument Check: Ensures a path is provided.
- Path Conversion: Converts relative paths to absolute paths (e.g.,
SimpleWpfApp\bin
toC:\path\to\SimpleWpfApp\bin
). - Deletion Attempt: Uses IObit Unlocker (
/delete
) to unlock and delete the target. - Fallback: If IObit Unlocker fails, it tries native CMD commands (
rd /s /q
for folders,del /f /q
for files). - Verification: Confirms deletion and provides feedback.
- Open CMD as Administrator:
cd path\to\script\directory
- Run the script with the target path:
Example:
ForceDelete.bat "path\to\file_or_folder"
ForceDelete.bat "C:\dev\Csharp\20250414\WpfTestingMWE\SimpleWpfApp\bin"
- Run CMD as Administrator to avoid permission issues.
- Ensure IObit Unlocker is installed at the default path.
- If the target is locked (e.g., by Visual Studio), close the locking process or reboot in Safe Mode before running the script.
- Tested on Tiny10, but should work on standard Windows 10/11 setups.
Developed with assistance from Grok 3 (xAI) on April 14, 2025.