Skip to content

Instantly share code, notes, and snippets.

@Foadsf
Last active April 14, 2025 14:50
Show Gist options
  • Save Foadsf/fbcc14b3368168e73259846a9fdee091 to your computer and use it in GitHub Desktop.
Save Foadsf/fbcc14b3368168e73259846a9fdee091 to your computer and use it in GitHub Desktop.

Force Delete Files/Folders on Windows with IObit Unlocker and a CMD Batch Script

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.

Step 1: Install IObit Unlocker via Chocolatey

  1. 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'))"
  2. 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.

Step 2: Create the Batch Script (ForceDelete.bat)

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.

Script Content

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

What the Script Does

  • Argument Check: Ensures a path is provided.
  • Path Conversion: Converts relative paths to absolute paths (e.g., SimpleWpfApp\bin to C:\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.

Step 3: Use the Script

  1. Open CMD as Administrator:
    cd path\to\script\directory
  2. Run the script with the target path:
    ForceDelete.bat "path\to\file_or_folder"
    Example:
    ForceDelete.bat "C:\dev\Csharp\20250414\WpfTestingMWE\SimpleWpfApp\bin"

Notes

  • 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.

Credits

Developed with assistance from Grok 3 (xAI) on April 14, 2025.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment