[.net] How to force uninstallation of windows service

I installed a windows service using installUtil.exe.

After updating the code I used installUtil.exe again to install the service w/o uninstalling the original version first.

When I now try to uninstall the service, installUtil.exe completes the uninstall successfully, but the service still appears.

If I try to change its properties, I receive the message 'service is marked for deletion'.

How can I force the deletion (preferrably w/o restarting the server)?

This question is related to .net windows-services

The answer is


It is also worth noting that this:

sc delete "ServiceName"

does not work in PowerShell, sc is an alias for the cmdlet Set-Content in PowerShell. You need to do:

sc.exe delete "ServiceName"

This worked for me

$a = Get-WmiObject Win32_Service | Where-Object {$_.Name -eq 'psexesvc'}
$a.Delete()

The following will work without restarting the machine:

  1. Search the Registry \ HKEY_LOCAL_MACHINE for < Your Service Name > (both keys and values)
  2. Set "Legacy" value to 0

Just in case this answer helps someone: as found here, you might save yourself a lot of trouble running Sysinternals Autoruns as administrator. Just go to the "Services" tab and delete your service.

It did the trick for me on a machine where I didn't have any permission to edit the registry.


You don't have to restart your machine. Start cmd or PowerShell in elevated mode.

sc.exe queryex <SERVICE_NAME>

Then you'll get some info. A PID number will show.

taskkill /pid <SERVICE_PID> /f

Where /f is to force stop.

Now you can install or launch your service.


Close cmd and services window if opened, then start cmd again by right clicking and selecting run as administrator. If sc delete serviceName does not work or anything does not work.

http://weblogs.asp.net/avnerk/archive/2007/09/05/windows-services-services-msc-and-the-quot-this-service-is-marked-for-deletion-quot-error.aspx


Have you try stopping the service before calling uninstall? I had this problem randomly. Sometime I could remove it without restarting. My guess is that it has to do with the service still running


I use the following PowerShell cobbled together from a few places for our Octopus Deploy instances when TopShelf messes up or a service fails for some other reason.

$ServiceName = 'MyNaughtyService'
$ServiceName | Stop-Service -ErrorAction SilentlyContinue
# We tried nicely, now KILL!!!
$ServiceNamePID = Get-Service | Where { $_.Name -eq $ServiceName} # If it was hung($_.Status -eq 'StopPending' -or $_.Status -eq 'Stopping') -and
$ServicePID = (Get-WmiObject Win32_Service | Where {$_.Name -eq $ServiceNamePID.Name}).ProcessID
Stop-Process $ServicePID -Force

Have you try stopping the service before calling uninstall? I had this problem randomly. Sometime I could remove it without restarting. My guess is that it has to do with the service still running


There are plenty of forum questions in that subject.

I have found the answer in windows api. You don't need to restart the computer after uninstalling the service. You have to call:

BOOL WINAPI CloseServiceHandle(
  SC_HANDLE hSCObject
);

That closes the handle of the service. On windows 7 it solved my problem. I do:

  • stop service
  • close handle
  • uninstall service
  • wait 3 sec
  • copy new exe to the directory
  • install the service
  • start service
  • close handle

I know this isn't going to help, but it might help someone in the future.

I've just had the same problem, closing and re-opening the services manager removed both the entry from the registry and completed the uninstall of the service.

Previous to that, refreshing the services manager hadn't helped.


If you can not stop the service in Services, you can stop your windows service exe in Task Manager. After that, you can remove the service.


Refreshing the service list always did it for me. If the services window is open, it will hold some memory of it existing for some reason. F5 and I'm reinstalling again!


well, you can use SC.EXE to delete any windows Service forcefully if un-install doesnt removes by any chance.

sc delete <Service_Name>

Read more on "MS Techno Blogging" Deleting Services Forcefully from Services MMC


There are plenty of forum questions in that subject.

I have found the answer in windows api. You don't need to restart the computer after uninstalling the service. You have to call:

BOOL WINAPI CloseServiceHandle(
  SC_HANDLE hSCObject
);

That closes the handle of the service. On windows 7 it solved my problem. I do:

  • stop service
  • close handle
  • uninstall service
  • wait 3 sec
  • copy new exe to the directory
  • install the service
  • start service
  • close handle

Unfortunately, you need to restart the server. That should remove the "deleted" service.


I know this isn't going to help, but it might help someone in the future.

I've just had the same problem, closing and re-opening the services manager removed both the entry from the registry and completed the uninstall of the service.

Previous to that, refreshing the services manager hadn't helped.


Close cmd and services window if opened, then start cmd again by right clicking and selecting run as administrator. If sc delete serviceName does not work or anything does not work.

http://weblogs.asp.net/avnerk/archive/2007/09/05/windows-services-services-msc-and-the-quot-this-service-is-marked-for-deletion-quot-error.aspx


The following will work without restarting the machine:

  1. Search the Registry \ HKEY_LOCAL_MACHINE for < Your Service Name > (both keys and values)
  2. Set "Legacy" value to 0

Have you try stopping the service before calling uninstall? I had this problem randomly. Sometime I could remove it without restarting. My guess is that it has to do with the service still running


I use the following PowerShell cobbled together from a few places for our Octopus Deploy instances when TopShelf messes up or a service fails for some other reason.

$ServiceName = 'MyNaughtyService'
$ServiceName | Stop-Service -ErrorAction SilentlyContinue
# We tried nicely, now KILL!!!
$ServiceNamePID = Get-Service | Where { $_.Name -eq $ServiceName} # If it was hung($_.Status -eq 'StopPending' -or $_.Status -eq 'Stopping') -and
$ServicePID = (Get-WmiObject Win32_Service | Where {$_.Name -eq $ServiceNamePID.Name}).ProcessID
Stop-Process $ServicePID -Force

Also make sure that there are no instances of the executable still active (perhaps one that might have been running, for whatever reason, independently of the service).

I was opening and closing MMC and looking for the PIDs to kill - but when looking in process explorer there were a couple of extant processes running from a forgotten scheduled batch. Killed them. Job done.


You don't have to restart your machine. Start cmd or PowerShell in elevated mode.

sc.exe queryex <SERVICE_NAME>

Then you'll get some info. A PID number will show.

taskkill /pid <SERVICE_PID> /f

Where /f is to force stop.

Now you can install or launch your service.


Just in case this answer helps someone: as found here, you might save yourself a lot of trouble running Sysinternals Autoruns as administrator. Just go to the "Services" tab and delete your service.

It did the trick for me on a machine where I didn't have any permission to edit the registry.


sc delete sericeName

Just make sure the service is stopped before doing this. I have seen this work most times. There are times where I have seen windows get stuck on something and it insists on a reboot.


Unfortunately, you need to restart the server. That should remove the "deleted" service.


It is also worth noting that this:

sc delete "ServiceName"

does not work in PowerShell, sc is an alias for the cmdlet Set-Content in PowerShell. You need to do:

sc.exe delete "ServiceName"

I am late, but would like to add an alternative, which may look strange, but I didn't see another way:

As I install my Windows Services in a CI process each night, I needed something that works all the time and is completely automated. For some reason, the services were always marked for deletion for a long time (5 minutes or more) after uninstalling them. Therefore, I extended the reinstallation batch script to make sure that the service is really deleted (simplified version):

REM Stop the service first
net stop My-Socket-Server

REM Same as installutil.exe, just implemented in the service
My.Socket.Server.exe /u

:loop1
    REM Easy way to wait for 5 seconds
    ping 192.0.2.2 -n 1 -w 5000 > nul
    sc delete My-Socket-Server
    echo %date% %time%: Trying to delete service.
    if errorlevel 1072 goto :loop1

REM Just for output purposes, typically I get that the service does not exist
sc query My-Socket-Server

REM Installing the new service, same as installutil.exe but in code
My.Socket.Server.exe /i

REM Start the new service
net start My-Socket-Server

What I can see, is that the service is marked for deletion for about 5 minutes (!) until it finally goes through. Finally, I don't need any more manual interventions. I will extend the script in the future so that something happens after a certain time (e.g. notification after 30 minutes).


sc delete sericeName

Just make sure the service is stopped before doing this. I have seen this work most times. There are times where I have seen windows get stuck on something and it insists on a reboot.


If you can not stop the service in Services, you can stop your windows service exe in Task Manager. After that, you can remove the service.


You can manually delete the registry key for your service, located at HKLM\SYSTEM\CurrentControlSet\Services, after that you won't see the service in service control manager (need to refresh or reopen to see the change), and you can reinstall the service as usual.


Unfortunately, you need to restart the server. That should remove the "deleted" service.


You can manually delete the registry key for your service, located at HKLM\SYSTEM\CurrentControlSet\Services, after that you won't see the service in service control manager (need to refresh or reopen to see the change), and you can reinstall the service as usual.


I am late, but would like to add an alternative, which may look strange, but I didn't see another way:

As I install my Windows Services in a CI process each night, I needed something that works all the time and is completely automated. For some reason, the services were always marked for deletion for a long time (5 minutes or more) after uninstalling them. Therefore, I extended the reinstallation batch script to make sure that the service is really deleted (simplified version):

REM Stop the service first
net stop My-Socket-Server

REM Same as installutil.exe, just implemented in the service
My.Socket.Server.exe /u

:loop1
    REM Easy way to wait for 5 seconds
    ping 192.0.2.2 -n 1 -w 5000 > nul
    sc delete My-Socket-Server
    echo %date% %time%: Trying to delete service.
    if errorlevel 1072 goto :loop1

REM Just for output purposes, typically I get that the service does not exist
sc query My-Socket-Server

REM Installing the new service, same as installutil.exe but in code
My.Socket.Server.exe /i

REM Start the new service
net start My-Socket-Server

What I can see, is that the service is marked for deletion for about 5 minutes (!) until it finally goes through. Finally, I don't need any more manual interventions. I will extend the script in the future so that something happens after a certain time (e.g. notification after 30 minutes).


sc delete sericeName

Just make sure the service is stopped before doing this. I have seen this work most times. There are times where I have seen windows get stuck on something and it insists on a reboot.


well, you can use SC.EXE to delete any windows Service forcefully if un-install doesnt removes by any chance.

sc delete <Service_Name>

Read more on "MS Techno Blogging" Deleting Services Forcefully from Services MMC


This worked for me

$a = Get-WmiObject Win32_Service | Where-Object {$_.Name -eq 'psexesvc'}
$a.Delete()

Unfortunately, you need to restart the server. That should remove the "deleted" service.


Have you try stopping the service before calling uninstall? I had this problem randomly. Sometime I could remove it without restarting. My guess is that it has to do with the service still running


Refreshing the service list always did it for me. If the services window is open, it will hold some memory of it existing for some reason. F5 and I'm reinstalling again!


Also make sure that there are no instances of the executable still active (perhaps one that might have been running, for whatever reason, independently of the service).

I was opening and closing MMC and looking for the PIDs to kill - but when looking in process explorer there were a couple of extant processes running from a forgotten scheduled batch. Killed them. Job done.


Examples related to .net

You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to use Bootstrap 4 in ASP.NET Core No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization .net Core 2.0 - Package was restored using .NetFramework 4.6.1 instead of target framework .netCore 2.0. The package may not be fully compatible Update .NET web service to use TLS 1.2 EF Core add-migration Build Failed What is the difference between .NET Core and .NET Standard Class Library project types? Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies Nuget connection attempt failed "Unable to load the service index for source" Token based authentication in Web API without any user interface

Examples related to windows-services

Can't start Tomcat as Windows Service Error 1053 the service did not respond to the start or control request in a timely fashion How to solve "The specified service has been marked for deletion" error Service will not start: error 1067: the process terminated unexpectedly How to get all Windows service names starting with a common word? Windows service with timer Windows service on Local Computer started and then stopped error Windows service start failure: Cannot start service from the command line or debugger "Automatic" vs "Automatic (Delayed start)" How to install node.js as windows service?