Skip to main content

How to Delete a Service in a Batch Script

A Windows service is a program that runs in the background, independent of any user being logged in. While services are typically managed by installers, there are times when a system administrator needs to manually remove one. This might be required when decommissioning an application, cleaning up after a faulty uninstaller, or removing a service that is no longer needed.

This guide will teach you how to delete a Windows service from the command line using the powerful, built-in sc.exe (Service Control) utility. You will learn the correct syntax for the delete command, the essential prerequisite of stopping the service first, and the critical importance of running the script with full administrator privileges.

danger

CRITICAL WARNING: Deleting a service is an irreversible system-level change. Accidentally deleting a critical Windows service could render your system unstable or unbootable. Always be absolutely certain of the service name before running this command. It is highly recommended to have a full system backup. This script must be run with full administrator privileges.

The Core Command: sc.exe

The sc.exe (Service Control) utility is the definitive command-line tool for managing all aspects of Windows services. It can create, query, configure, start, stop, and delete services.

Its commands are simple verbs. For our purpose, we will use stop and delete.

The Essential Prerequisite: Stopping the Service First

You cannot delete a service while it is running. The very first step in your script must be to stop the service. This is done with the sc stop command.

Syntax: sc stop "ServiceName"

  • "ServiceName": This is the internal service name, not the "Display Name." You must get this exact name first.

The Command to Delete a Service

Once the service is stopped, you can use the sc delete command to permanently remove it from the Service Control Manager database and the registry.

Syntax: sc delete "ServiceName"

This command removes the service registration. It does not delete the executable file on the disk that the service was running.

For example, this script stops and then deletes a service with the internal name "MyCustomSvc".

@ECHO OFF
REM This script MUST be run as an Administrator.

SET "ServiceName=MyCustomSvc"

ECHO --- Deleting Windows Service: %ServiceName% ---
ECHO WARNING: This is an irreversible action.
PAUSE
ECHO.

ECHO Step 1: Stopping the service...
sc stop "%ServiceName%"

ECHO.
ECHO Step 2: Deleting the service...
sc delete "%ServiceName%"

IF %ERRORLEVEL% EQU 0 (
ECHO [SUCCESS] Service "%ServiceName%" has been deleted.
) ELSE (
ECHO [FAILURE] An error occurred. Check if the service exists or if you are running as Admin.
)
note

The sc stop command may report an error if the service was already stopped. A robust script can check for this.

How to Get the Exact Service Name

The sc delete command requires the Service Name, not the friendly "Display Name" you see in the services.msc GUI. They are often different. For example, the "Windows Update" service has a service name of "wuauserv".

The best way to get the exact service name is with sc query.

Command to Find a Service: sc queryex type=service state=all | findstr /I "Display Name To Find"

C:\> sc queryex type=service state=all | findstr /I "Windows Update"

DISPLAY_NAME: Windows Update
SERVICE_NAME: wuauserv

This tells you that the correct service name to use in your script is wuauserv.

Common Pitfalls and How to Solve Them

  • Administrator Rights: This is the number one cause of failure. You will receive an "Access is denied" error if your script is not run from an elevated command prompt. Solution: You must run the script as an Administrator.

  • Service is Still Running: If you try to delete a service before it has fully stopped, the command will fail. Some services take several seconds to shut down. Solution: The most robust scripts will poll the service's status after issuing a stop command and wait until it reports "STOPPED" before proceeding to the delete command.

  • Incorrect Service Name: Using the Display Name instead of the Service Name will result in a "The specified service does not exist as an installed service" error. Solution: Always use sc queryex to find the correct internal service name first.

Practical Example: A Full Cleanup Script for a Custom Service

This script provides a robust and reusable template for completely removing a custom service. It includes the correct stop/delete sequence and error checking.

@ECHO OFF
SETLOCAL
REM This script must be run as an Administrator.

SET "ServiceName=MyCustomWebApp"

ECHO --- Full Service Removal Script ---
ECHO Target Service: %ServiceName%
ECHO.

REM --- Step 1: Verify the service exists ---
sc query "%ServiceName%" > NUL
IF %ERRORLEVEL% NEQ 0 (
ECHO [INFO] Service "%ServiceName%" does not exist. Nothing to do.
GOTO :End
)

REM --- Step 2: Stop the service if it's running ---
sc query "%ServiceName%" | FIND "STATE" | FIND "RUNNING" > NUL
IF %ERRORLEVEL% EQU 0 (
ECHO Service is running. Attempting to stop it...
sc stop "%ServiceName%"

REM Optional: Add a short wait for the service to stop.
TIMEOUT /T 5 > NUL
) ELSE (
ECHO Service is already stopped.
)

ECHO.
ECHO --- Step 3: Delete the service ---
ECHO Deleting service registration...
sc delete "%ServiceName%"

IF %ERRORLEVEL% EQU 0 (
ECHO [SUCCESS] Service "%ServiceName%" has been deleted.
) ELSE (
ECHO [FAILURE] Failed to delete the service.
)

:End
ENDLOCAL

Conclusion

The sc.exe utility is the definitive command-line tool for managing Windows services. Deleting a service is a straightforward but high-risk operation that should be performed with care.

For a successful and safe deletion:

  1. Always run your script as an Administrator.
  2. Use sc queryex to find the exact internal Service Name.
  3. Stop the service first using sc stop "ServiceName".
  4. Delete the service using sc delete "ServiceName".
  5. Remember that this does not delete the service's executable file, which must be cleaned up separately if required.