How to Delete a Partition with DiskPart in Batch Script
When you need to reclaim disk space, remove an unwanted recovery partition, or prepare a drive for a fresh layout, deleting existing partitions is the first step. The graphical Disk Management tool often refuses to delete certain protected partitions (like OEM recovery or EFI system partitions). DiskPart can bypass these restrictions with the delete partition override command, giving you full control over every partition on the disk, regardless of manufacturer-set protection flags.
This guide will explain how to remove partitions programmatically using DiskPart from Batch.
Critical Safety Warning
Deleting a partition permanently destroys ALL data on that volume. There is no confirmation prompt from DiskPart, no undo, and no recycle bin.
Before running any partition deletion script:
- Back up any data on the partition you intend to delete.
- Run
list partitionto verify partition numbers, sizes, and types. - Identify the partition by SIZE and TYPE, not just by number. Partition numbers can shift after previous deletions.
- Never delete the EFI System Partition on a boot disk unless you are completely wiping the disk. Deleting it makes the system unbootable.
- Never delete the Windows partition (usually the largest partition on Disk 0) unless you intend to destroy the OS installation.
Understanding Partition Types
When you run list partition in DiskPart, the Type column tells you what each partition is for:
| Type | Meaning | Safe to Delete? |
|---|---|---|
| Primary | Standard data or OS partition | Check contents first |
| System | EFI System Partition (ESP) contains boot files | NO (unless wiping entire disk) |
| Recovery | Windows Recovery Environment (WinRE) | Usually safe, but prevents push-button recovery |
| Reserved | Microsoft Reserved Partition (MSR) | NO (needed for GPT disk management) |
| OEM | Manufacturer recovery/diagnostics | Usually safe, but prevents factory reset |
Method 1: Interactive Partition Deletion with Safety Checks
This method lists all partitions on a disk, shows their sizes and types, and requires explicit confirmation before deleting.
Implementation
@echo off
setlocal EnableDelayedExpansion
:: Verify admin privileges
net session >nul 2>&1
if errorlevel 1 (
echo [ERROR] DiskPart requires administrator privileges. >&2
endlocal
exit /b 1
)
:: =============================================
:: Step 1: Select the disk
:: =============================================
echo.
echo ============================================================
echo PARTITION DELETION TOOL
echo ============================================================
echo.
echo [INFO] Connected disks:
echo.
set "DPList=%TEMP%\dp_list_%RANDOM%.txt"
(echo list disk^) > "%DPList%"
diskpart /s "%DPList%" 2>nul
del "%DPList%" 2>nul
echo.
set /p "DiskNum=Enter disk number (or Q to quit): "
if /i "%DiskNum%"=="Q" (
echo [INFO] Cancelled.
endlocal
exit /b 0
)
if "%DiskNum%"=="" (
echo [ERROR] No disk number entered. >&2
endlocal
exit /b 1
)
:: =============================================
:: Step 2: List partitions on the selected disk
:: =============================================
echo.
echo [INFO] Partitions on Disk %DiskNum%:
echo.
set "DPParts=%TEMP%\dp_parts_%RANDOM%.txt"
(
echo select disk %DiskNum%
echo list partition
) > "%DPParts%"
diskpart /s "%DPParts%" 2>nul
del "%DPParts%" 2>nul
:: Also show detailed info via PowerShell
echo.
echo [INFO] Detailed partition information:
echo.
powershell -NoProfile -Command ^
"$parts = Get-Partition -DiskNumber %DiskNum% -ErrorAction SilentlyContinue;" ^
"if ($parts) {" ^
" $parts | ForEach-Object {" ^
" $sizeMB = [math]::Round($_.Size / 1MB);" ^
" $sizeStr = if ($sizeMB -ge 1024) { '{0:N1} GB' -f ($_.Size / 1GB) } else { \"$sizeMB MB\" };" ^
" $letter = if ($_.DriveLetter) { \"$($_.DriveLetter):\" } else { '(none)' };" ^
" [PSCustomObject]@{" ^
" 'Part #' = $_.PartitionNumber;" ^
" 'Drive' = $letter;" ^
" 'Size' = $sizeStr;" ^
" 'Type' = $_.Type;" ^
" 'GptType' = if ($_.GptType) { switch -Regex ($_.GptType) {" ^
" 'c12a7328' { 'EFI System' }" ^
" 'e3c9e316' { 'Microsoft Reserved' }" ^
" 'de94bba4' { 'Windows Recovery' }" ^
" 'ebd0a0a2' { 'Basic Data' }" ^
" default { 'Other' }" ^
" }} else { 'MBR' }" ^
" }" ^
" } | Format-Table -AutoSize" ^
"} else {" ^
" Write-Host 'No partitions found on this disk.'" ^
"}"
:: =============================================
:: Step 3: Get partition number to delete
:: =============================================
echo.
set /p "PartNum=Enter partition number to delete (or Q to quit): "
if /i "%PartNum%"=="Q" (
echo [INFO] Cancelled.
endlocal
exit /b 0
)
if "%PartNum%"=="" (
echo [ERROR] No partition number entered. >&2
endlocal
exit /b 1
)
:: =============================================
:: Step 4: Safety checks
:: =============================================
:: Check if this is a system/boot partition
set "IsCritical=FALSE"
powershell -NoProfile -Command ^
"$p = Get-Partition -DiskNumber %DiskNum% -PartitionNumber %PartNum% -ErrorAction SilentlyContinue;" ^
"if (-not $p) { exit 2 };" ^
"if ($p.IsSystem -or $p.IsBoot -or $p.IsActive) { exit 1 };" ^
"if ($p.GptType -match 'c12a7328|e3c9e316') { exit 1 };" ^
"exit 0" >nul 2>&1
set "CheckResult=%errorlevel%"
if %CheckResult% equ 2 (
echo [ERROR] Partition %PartNum% does not exist on Disk %DiskNum%. >&2
endlocal
exit /b 1
)
if %CheckResult% equ 1 (
set "IsCritical=TRUE"
echo.
echo ============================================================
echo *** WARNING: CRITICAL PARTITION DETECTED ***
echo ============================================================
echo.
echo Partition %PartNum% is a SYSTEM, BOOT, or PROTECTED partition.
echo Deleting it may make the computer UNBOOTABLE.
echo.
echo Only proceed if you are COMPLETELY WIPING this disk.
echo.
echo ============================================================
)
:: =============================================
:: Step 5: Confirm deletion
:: =============================================
echo.
if "!IsCritical!"=="TRUE" (
echo [WARNING] This is a critical/protected partition.
set /p "Confirm=Type DELETE to force-remove this partition: "
) else (
set /p "Confirm=Type DELETE to remove Partition %PartNum% from Disk %DiskNum%: "
)
if /i not "!Confirm!"=="DELETE" (
echo [INFO] Cancelled. No changes were made.
endlocal
exit /b 0
)
:: =============================================
:: Step 6: Execute deletion
:: =============================================
:: Use override for critical partitions, standard delete otherwise
if "!IsCritical!"=="TRUE" (
set "DeleteCmd=delete partition override"
) else (
set "DeleteCmd=delete partition"
)
echo.
echo [ACTION] Deleting Partition %PartNum% on Disk %DiskNum%...
set "DPScript=%TEMP%\dp_delpart_%RANDOM%.txt"
(
echo select disk %DiskNum%
echo select partition %PartNum%
echo !DeleteCmd!
) > "%DPScript%"
diskpart /s "%DPScript%" >nul 2>&1
set "DPResult=%errorlevel%"
del "%DPScript%" 2>nul
if %DPResult% neq 0 (
echo [ERROR] Partition deletion failed. >&2
echo The partition may be in use or the disk may be write-protected. >&2
endlocal
exit /b 1
)
echo [OK] Partition %PartNum% deleted. The space is now unallocated.
:: Log the operation
for /f "delims=" %%t in (
'powershell -NoProfile -Command "Get-Date -Format ''yyyy-MM-dd HH:mm:ss''"'
) do set "Timestamp=%%t"
echo [!Timestamp!] Deleted Partition %PartNum% on Disk %DiskNum% (!DeleteCmd!^) by %USERNAME% >> "%~dp0partition_changes.log"
echo [INFO] Operation logged to: partition_changes.log
endlocal
exit /b 0
Why delete partition override is needed for protected partitions:
Standard delete partition refuses to remove partitions flagged as System, Recovery, OEM, or Reserved. These flags are set by Windows and hardware manufacturers to prevent accidental deletion. delete partition override bypasses all protection flags. The interactive method uses override automatically when a critical partition is detected, but only after an extra warning.
Why the script detects critical partitions automatically:
Rather than relying on the user to know which partitions are critical, the script uses Get-Partition to check IsSystem, IsBoot, IsActive, and the GPT type GUID. If any of these indicate a system-critical partition, the script displays an additional warning before allowing deletion. This catches the most dangerous mistake: accidentally deleting the EFI System Partition or the Windows boot partition.
Method 2: Scripted Partition Deletion (for Automation)
For deployment pipelines or automated disk preparation where the disk and partition are known.
@echo off
setlocal
set "DiskNum=%~1"
set "PartNum=%~2"
if "%PartNum%"=="" (
echo Usage: %~nx0 ^<disk_number^> ^<partition_number^> [--override]
echo.
echo --override Force-delete protected/system partitions
echo.
echo Example:
echo %~nx0 1 3
echo %~nx0 1 4 --override
endlocal
exit /b 1
)
net session >nul 2>&1
if errorlevel 1 (
echo [ERROR] Administrator privileges required. >&2
endlocal
exit /b 1
)
:: Check for --override flag
set "DeleteCmd=delete partition"
for %%a in (%*) do (
if /i "%%~a"=="--override" set "DeleteCmd=delete partition override"
)
:: Verify partition exists
powershell -NoProfile -Command ^
"Get-Partition -DiskNumber %DiskNum% -PartitionNumber %PartNum% -ErrorAction SilentlyContinue | Out-Null;" ^
"if (-not $?) { exit 1 } else { exit 0 }" >nul 2>&1
if errorlevel 1 (
echo [ERROR] Partition %PartNum% on Disk %DiskNum% does not exist. >&2
endlocal
exit /b 1
)
:: Get partition details for logging
for /f "tokens=1-2 delims=|" %%a in (
'powershell -NoProfile -Command ^
"$p = Get-Partition -DiskNumber %DiskNum% -PartitionNumber %PartNum%;" ^
"$sizeMB = [math]::Round($p.Size / 1MB);" ^
"Write-Output \"$($p.Type)|$sizeMB\""'
) do (
set "PartType=%%a"
set "PartSizeMB=%%b"
)
echo [ACTION] Deleting Partition %PartNum% on Disk %DiskNum% (%PartType%, %PartSizeMB% MB^)...
set "DPScript=%TEMP%\dp_del_%RANDOM%.txt"
(
echo select disk %DiskNum%
echo select partition %PartNum%
echo %DeleteCmd%
) > "%DPScript%"
diskpart /s "%DPScript%" >nul 2>&1
set "DPResult=%errorlevel%"
del "%DPScript%" 2>nul
if %DPResult% neq 0 (
if "%DeleteCmd%"=="delete partition" (
echo [ERROR] Deletion failed. This may be a protected partition. >&2
echo Add --override flag to force-delete: %~nx0 %DiskNum% %PartNum% --override >&2
) else (
echo [ERROR] Deletion failed even with override. >&2
echo The partition may be in use or the disk may be write-protected. >&2
)
endlocal
exit /b 1
)
echo [OK] Partition %PartNum% deleted (%PartType%, %PartSizeMB% MB^). Space is now unallocated.
:: Log the operation
for /f "delims=" %%t in (
'powershell -NoProfile -Command "Get-Date -Format ''yyyy-MM-dd HH:mm:ss''"'
) do echo [%%t] Deleted Part %PartNum% on Disk %DiskNum% (%PartType%, %PartSizeMB% MB^) [%DeleteCmd%] by %USERNAME% >> "%~dp0partition_changes.log"
endlocal
exit /b 0
Using --override:
The --override flag is deliberately not the default. Protected partitions are protected for a reason: accidentally deleting the EFI System Partition makes the system unbootable. Requiring the explicit flag forces the operator to acknowledge they are bypassing safety protections.
Method 3: Delete All Partitions on a Data Disk
When preparing a data disk for a fresh layout, this method removes all existing partitions in one operation.
@echo off
setlocal EnableDelayedExpansion
set "DiskNum=%~1"
if "%DiskNum%"=="" (
echo Usage: %~nx0 ^<disk_number^>
echo.
echo Removes ALL partitions from the specified disk.
echo *** ALL DATA ON THE DISK WILL BE PERMANENTLY DESTROYED ***
echo.
echo Example: %~nx0 2
endlocal
exit /b 1
)
net session >nul 2>&1
if errorlevel 1 (
echo [ERROR] Administrator privileges required. >&2
endlocal
exit /b 1
)
if "%DiskNum%"=="0" (
echo [ERROR] Refusing to wipe Disk 0 (OS drive^). >&2
echo Use "diskpart" interactively if you truly need to wipe the OS disk. >&2
endlocal
exit /b 1
)
:: Get disk details
for /f "tokens=1-2 delims=|" %%a in (
'powershell -NoProfile -Command ^
"$d = Get-Disk -Number %DiskNum% -ErrorAction SilentlyContinue;" ^
"if (-not $d) { Write-Output ''NOT_FOUND|0''; exit 1 };" ^
"Write-Output \"$($d.FriendlyName)|$([math]::Round($d.Size/1GB,1))\""'
) do (
set "DiskName=%%a"
set "DiskSizeGB=%%b"
)
if "!DiskName!"=="NOT_FOUND" (
echo [ERROR] Disk %DiskNum% not found. >&2
endlocal
exit /b 1
)
:: Count existing partitions
for /f "delims=" %%n in (
'powershell -NoProfile -Command ^
"(Get-Partition -DiskNumber %DiskNum% -ErrorAction SilentlyContinue).Count"'
) do set "PartCount=%%n"
if "%PartCount%"=="" set "PartCount=0"
if %PartCount% equ 0 (
echo [INFO] Disk %DiskNum% has no partitions.
endlocal
exit /b 0
)
echo [INFO] Disk %DiskNum%: !DiskName! (!DiskSizeGB! GB, %PartCount% partition(s)^)
echo.
echo [WARNING] ALL %PartCount% partition(s^) will be deleted.
echo [WARNING] ALL DATA on Disk %DiskNum% will be PERMANENTLY DESTROYED.
echo.
set /p "Confirm=Type WIPE to proceed: "
if /i not "!Confirm!"=="WIPE" (
echo [INFO] Cancelled.
endlocal
exit /b 0
)
echo [ACTION] Removing all partitions from Disk %DiskNum%...
:: Use clean to remove all partitions in one operation
set "DPScript=%TEMP%\dp_cleanall_%RANDOM%.txt"
(
echo select disk %DiskNum%
echo clean
) > "%DPScript%"
diskpart /s "%DPScript%" >nul 2>&1
set "DPResult=%errorlevel%"
del "%DPScript%" 2>nul
if %DPResult% neq 0 (
echo [ERROR] Failed to remove partitions. Disk may be in use. >&2
endlocal
exit /b 1
)
echo [OK] All partitions removed from Disk %DiskNum%. Disk is now unallocated.
for /f "delims=" %%t in (
'powershell -NoProfile -Command "Get-Date -Format ''yyyy-MM-dd HH:mm:ss''"'
) do echo [%%t] Cleaned Disk %DiskNum% (!DiskName!, !DiskSizeGB! GB, %PartCount% partitions removed^) by %USERNAME% >> "%~dp0partition_changes.log"
endlocal
exit /b 0
Why clean instead of deleting partitions one by one:
DiskPart's clean command removes ALL partitions and volume structures in a single operation, including protected partitions that would require override individually. This is faster and more reliable than looping through partitions. The trade-off is that it's all-or-nothing: you cannot selectively keep some partitions.
How to Avoid Common Errors
Wrong Way: Deleting the EFI System Partition on the Boot Disk
:: CATASTROPHIC: makes the system unbootable
select disk 0
select partition 1
delete partition override
The EFI System Partition contains the UEFI boot loader. Without it, the computer cannot find Windows and will display "No bootable device found."
Correct Way: Only delete the ESP if you are completely wiping the disk for reuse. Method 1 detects system-critical partitions and displays an extra warning before allowing deletion.
Wrong Way: Deleting by Partition Number Without Verification
Partition numbers can change after previous deletions. If you delete Partition 3, the former Partition 4 becomes the new Partition 3. A script that hardcodes partition numbers may delete the wrong partition on the second run.
Correct Way: Always run list partition and verify by size and type immediately before deleting. Method 1 includes this step.
Problem: "Cannot Delete a Protected Partition"
Standard delete partition refuses to remove partitions flagged as System, Recovery, OEM, or Reserved.
Solution: Use delete partition override to bypass protection flags. Method 2's --override flag provides this explicitly. Method 1 uses it automatically when a critical partition is detected (after an extra warning).
Problem: Partition Is In Use
If an application has files open on the partition, or the partition has a drive letter with active file handles, DiskPart may refuse to delete it.
Solution: Close all applications using the partition, remove its drive letter first, and try again:
:: Remove the drive letter before deleting
select disk %DiskNum%
select partition %PartNum%
remove letter=D
delete partition
Problem: After Deletion, Space Shows as "Unallocated" but Cannot Be Used
After deleting a partition, the space appears as "Unallocated" in Disk Management. It cannot store files until a new partition is created on it.
Solution: After deletion, either create a new partition in the unallocated space or extend an adjacent partition to absorb it:
:: Extend the adjacent partition to use the freed space
select disk %DiskNum%
select partition %AdjacentPartNum%
extend
Best Practices and Rules
1. Always List Before Deleting
Run list partition and verify the target by size and type immediately before deleting. Partition numbers are positional and change when partitions are added or removed.
2. Back Up Before Deleting
Even if you think the partition is empty, verify first. Hidden system files, recovery data, or application configurations may be stored on partitions that appear unused.
3. Log Every Deletion
For compliance and audit purposes, record every partition deletion with the disk number, partition number, type, size, and operator. All methods in this guide include automatic logging.
4. Use override Only When Necessary
The delete partition override command bypasses safety protections that exist for good reasons. Use it only when you have confirmed the partition is safe to remove (e.g., an old OEM recovery partition you no longer need).
5. Consider Using clean for Full Disk Preparation
If you need to remove ALL partitions from a data disk, clean (Method 3) is faster and simpler than deleting partitions individually. It handles protected partitions automatically.
6. Extend Adjacent Partitions After Deletion
Deleted partitions leave unallocated space. If you don't plan to create new partitions, extend an adjacent volume to reclaim the space:
select volume D
extend
Conclusions
Deleting partitions via DiskPart provides full control over disk layout, including the ability to remove manufacturer-protected partitions that the graphical Disk Management tool refuses to touch. By implementing proper safety checks (like partition type detection, critical partition warnings, explicit confirmation prompts, and mandatory logging), you protect against the most dangerous mistakes while maintaining the flexibility to reshape your storage as needed.