How to Delete a Website in Internet Information Services (IIS) from Batch Script
Removing obsolete, decommissioned, or misconfigured websites from IIS is an essential cleanup task. Leaving abandoned sites running wastes server resources, can create security vulnerabilities through unmaintained code, and clutters the IIS Manager for administrators. Automating the deletion process via Batch Script ensures consistent, repeatable cleanup across server farms.
In this guide, we will explore how to safely delete IIS websites using the appcmd.exe command-line tool, including pre-deletion checks, associated application pool cleanup, and content removal.
Prerequisites
- IIS must be installed with
appcmd.exeavailable at%SystemRoot%\System32\inetsrv\. - Administrator privileges are required for all site management operations.
Method 1: Simple Website Deletion
The most straightforward deletion uses a single appcmd delete site command.
@echo off
setlocal
set "appcmd=%SystemRoot%\System32\inetsrv\appcmd.exe"
set "site_name=OldTestSite"
:: Verify Admin Privileges
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Administrator privileges required.
pause
exit /b 1
)
:: Verify appcmd exists
if not exist "%appcmd%" (
echo [ERROR] appcmd.exe not found at: %appcmd%
echo IIS may not be installed or Management Tools are missing.
pause
exit /b 1
)
:: Check if the site exists before attempting deletion
"%appcmd%" list site /name:"%site_name%" >nul 2>&1
if %errorlevel% neq 0 (
echo [INFO] Website "%site_name%" does not exist. Nothing to delete.
pause
exit /b 0
)
echo Deleting website "%site_name%"...
"%appcmd%" delete site /site.name:"%site_name%"
if %errorlevel% equ 0 (
echo [SUCCESS] Website "%site_name%" has been deleted from IIS.
) else (
echo [ERROR] Failed to delete website "%site_name%".
echo Check the System Event Log for details.
)
pause
exit /b 0
What Gets Deleted?
When you delete a site with appcmd, only the IIS configuration entry is removed. The following are not deleted:
- The physical files (HTML, CSS, JS, etc.) in the site's directory
- The associated application pool
- Any SSL certificates bound to the site
- DNS records pointing to the server
You must clean these up separately if needed.
Method 2: Complete Cleanup (Site + Pool + Files)
For a thorough decommission, you must delete the IIS site, its dedicated application pool, and optionally the content directory. This script fetches the necessary properties before deleting the site so it knows what to clean up afterward.
@echo off
setlocal enabledelayedexpansion
set "appcmd=%SystemRoot%\System32\inetsrv\appcmd.exe"
set "site_name=DecommissionedApp"
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)
if not exist "%appcmd%" (
echo [ERROR] appcmd.exe not found. IIS may not be installed.
pause
exit /b 1
)
echo =============================================
echo DECOMMISSIONING: %site_name%
echo =============================================
echo.
:: 1. Check if the site exists
"%appcmd%" list site /name:"%site_name%" >nul 2>&1
if !errorlevel! neq 0 (
echo [INFO] Site "%site_name%" does not exist in IIS.
pause
exit /b 0
)
:: 2. Get the physical path of the root virtual directory before deleting
set "site_path="
for /f "tokens=*" %%L in ('"%appcmd%" list vdir /app.name:"%site_name%/" /text:physicalPath 2^>nul') do set "site_path=%%L"
:: 3. Get the application pool name of the root application
set "pool_name="
for /f "tokens=*" %%P in ('"%appcmd%" list app /app.name:"%site_name%/" /text:applicationPool 2^>nul') do set "pool_name=%%P"
echo Site Path: !site_path!
echo App Pool: !pool_name!
echo.
:: 4. Stop the site first to release file locks
echo Stopping site...
"%appcmd%" stop site /site.name:"%site_name%" >nul 2>&1
:: 5. Delete the site from IIS
echo Deleting site from IIS...
"%appcmd%" delete site /site.name:"%site_name%" >nul
if !errorlevel! neq 0 (
echo [ERROR] Failed to delete site "%site_name%".
pause
exit /b 1
)
echo [OK] Site deleted.
:: 6. Delete the application pool (if it is not shared/default)
if defined pool_name (
if /i "!pool_name!"=="DefaultAppPool" (
echo [SKIP] Not deleting DefaultAppPool ^(shared pool^).
) else (
:: Check if any OTHER sites still use this pool
set "pool_in_use=0"
for /f "delims=" %%C in ('"%appcmd%" list app /applicationPool:"!pool_name!" 2^>nul') do (
set "pool_in_use=1"
)
if !pool_in_use! equ 1 (
echo [SKIP] App pool "!pool_name!" is still used by other applications.
) else (
echo Deleting app pool "!pool_name!"...
"%appcmd%" stop apppool /apppool.name:"!pool_name!" >nul 2>&1
"%appcmd%" delete apppool /apppool.name:"!pool_name!" >nul 2>&1
if !errorlevel! equ 0 (
echo [OK] App pool deleted.
) else (
echo [WARNING] Could not delete app pool "!pool_name!".
)
)
)
)
:: 7. Optionally remove the content directory
if defined site_path (
:: Safety rails: Prevent deletion of entire drives or default web root
if /i "!site_path!"=="%SystemDrive%\" (
echo [WARNING] Site path is the root drive. Skipping deletion for safety.
) else if /i "!site_path!"=="%SystemDrive%\inetpub\wwwroot" (
echo [WARNING] Site path is the default wwwroot. Skipping deletion for safety.
) else if exist "!site_path!" (
echo.
set "del_files="
set /p "del_files=Delete content permanently at !site_path!? (Y/N): "
if /i "!del_files!"=="Y" (
rmdir /s /q "!site_path!"
if exist "!site_path!" (
echo [WARNING] Directory could not be fully deleted ^(files may be locked^).
) else (
echo [OK] Content directory deleted.
)
) else (
echo [SKIP] Content directory preserved.
)
)
)
echo.
echo [COMPLETE] Decommission finished.
pause
The rmdir /s /q command permanently deletes the content directory without sending files to the Recycle Bin. In production environments, consider archiving or backing up the directory before deletion. You can export the site configuration first using:
"%appcmd%" list site /name:"%site_name%" /config /xml > "%site_name%_backup.xml"
Method 3: Deleting a Site with Interactive Confirmation
For helpdesk technicians, adding an interactive confirmation step prevents accidental deletion of the wrong site.
@echo off
setlocal enabledelayedexpansion
set "appcmd=%SystemRoot%\System32\inetsrv\appcmd.exe"
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)
:: List all sites for the user
echo Current IIS Websites:
echo =====================
"%appcmd%" list site
echo.
:: Prompt for site name
set "site_name="
set /p "site_name=Enter the site name to delete: "
if not defined site_name (
echo [CANCELLED] No site name entered.
pause
exit /b 0
)
:: Verify the site exists
"%appcmd%" list site /name:"!site_name!" >nul 2>&1
if !errorlevel! neq 0 (
echo [ERROR] Site '!site_name!' not found. Check the spelling.
pause
exit /b 1
)
:: Show details before confirming
echo.
echo Site details:
"%appcmd%" list site /name:"!site_name!"
echo.
:: Confirm with explicit YES requirement
set "confirm="
set /p "confirm=Type YES to permanently delete '!site_name!': "
if /i not "!confirm!"=="YES" (
echo [CANCELLED] No changes made.
pause
exit /b 0
)
:: Delete
"%appcmd%" delete site /site.name:"!site_name!"
if !errorlevel! equ 0 (
echo [SUCCESS] "!site_name!" deleted.
) else (
echo [ERROR] Deletion failed. Check the System Event Log for details.
)
pause
Method 4: Bulk Deletion of Multiple Sites
For cleaning up development or staging servers that have accumulated multiple test sites, you can iterate through a quoted list.
@echo off
setlocal enabledelayedexpansion
set "appcmd=%SystemRoot%\System32\inetsrv\appcmd.exe"
net session >nul 2>&1
if %errorlevel% neq 0 exit /b 1
:: Define sites to delete (Use quotes around each site name to handle spaces safely)
set sites="TestSite1" "TestSite2" "Old Staging App" "DevPreview"
echo Bulk deletion of IIS sites...
echo.
set "deleted=0"
set "skipped=0"
for %%S in (%sites%) do (
:: %%~S strips the quotes for our appcmd parameter
"%appcmd%" list site /name:"%%~S" >nul 2>&1
if !errorlevel! neq 0 (
echo [SKIP] "%%~S" ^(Not found^)
set /a "skipped+=1"
) else (
"%appcmd%" stop site /site.name:"%%~S" >nul 2>&1
"%appcmd%" delete site /site.name:"%%~S" >nul 2>&1
if !errorlevel! equ 0 (
echo [OK] "%%~S" deleted.
set /a "deleted+=1"
) else (
echo [FAIL] "%%~S" deletion failed.
set /a "skipped+=1"
)
)
)
echo.
echo Results: !deleted! deleted, !skipped! skipped/not found.
pause
Common Mistakes
The Wrong Way: Deleting by Site ID Instead of Name
:: RISKY - Site IDs can be reassigned if sites are created/deleted
"%appcmd%" delete site /site.id:3
Site IDs are auto-assigned integers that can change if other sites are added or removed. Using the numeric ID in automated scripts is dangerous because you might delete the wrong site. Always use the site name (/site.name:) for reliable targeting.
The Wrong Way: Querying Site for physicalPath
:: WRONG - A site object doesn't possess a physicalPath directly
"%appcmd%" list site /name:"OldApp" /text:physicalPath
Physical paths and App Pools belong to Applications and Virtual Directories, not the site shell itself. You must query the root application or root vdir to get this info (e.g., appcmd list vdir /app.name:"OldApp/" /text:physicalPath).
The Wrong Way: Deleting an App Pool Without Checking for Other Users
:: WRONG - Other sites may still be using this pool
"%appcmd%" delete apppool /apppool.name:"SharedPool"
Deleting an application pool that is still assigned to other websites will cause those sites to fail with a 503 Service Unavailable error. Always use appcmd list app /applicationPool:"SharedPool" to verify it's empty before issuing a deletion command.
Best Practices
- Always use site names, not IDs: Names are stable and descriptive; IDs are volatile and can be reassigned.
- Stop before deleting: Stop the site before deletion to ensure no active connections are interrupted unexpectedly and to release file locks on the physical directory.
- Clean up associated resources: After deleting the site, remove the dedicated application pool (if not shared) and optionally the content directory.
- Safety Check Physical Paths: Never blindly execute
rmdiron a path returned by IIS. Add fail-safes so you don't accidentally deleteC:\inetpub\wwwrootif a site was pointing to the default directory. - Never delete DefaultAppPool: If the target site uses the default pool, leave the pool in place for other sites that depend on it.
- Back up before deleting: In production, export the site configuration (
appcmd list site /config /xml) and archive the site files before any destructive operation.
Conclusion
Deleting a website from IIS via Batch Script can be done with a single appcmd delete site command, but a professional decommission process goes further. By incorporating accurate pre-deletion queries, safe application pool cleanup, optional content directory removal, and confirmation prompts, administrators can build robust scripts. For server farm management, bulk deletion from a predefined list of quoted site names ensures consistent cleanup across development, staging, and production environments.