How to uninstall a windows service and delete its files without rebooting

114,877

Solution 1

sc delete "service name"

will delete a service. I find that the sc utility is much easier to locate than digging around for installutil. Remember to stop the service if you have not already.

Solution 2

I had sort of the same problem as you. I have a system service that i want to uninstall and afterwards reinstall as part of an update. On certain systems this would not work without a reboot. The problem was that a call to DeleteService() would return ok, but the following call to CreateService() would tell me the service was still there, but marked for deletion (error code 1072). The registry would reflect that, since the subkey was still there (under HKLM\System\CurrentControlSet\Services), but "DeleteFlag" was set to 1. From that point on, only a reboot could fix the situation.

Some things that don't work:

  • Using "sc delete": it had the same problems as I. The call would return ok, but the service was not really gone and still in the registry with DeleteFlag = 1.
  • Deleting the key in the registry. The Service Manager seems to keep a database in memory and the registry is just a copy of it for the next boot.
  • Adding wait loops, waiting for .exe files to be ready to be overwritten, killing the process, etc.
  • Closing handles to the service. Which ones??

But here is what worked:

I noticed in some articles here on stackoverflow that net.exe has start/stop features as well (I only knew of sc.exe utility). And strangely enough, a "net stop svcname" plus a "sc delete svcname" worked! So net.exe must do something I don't do.

But net.exe doesn't contain an import to ControlService(), so how does it stop the service? I found out that net.exe spawns net1.exe, but net1.exe doesn't import ControlService() as well. I used the great API Monitor utility ( http://www.rohitab.com/apimonitor ) to see what net1.exe is doing, but it never called anything that looked promising.

But then I saw that it imports NetServiceControl() from NETAPI32.DLL (that had at least "Service" in its name!). MSDN says that this function is obsolete. Nevertheless, I found the prototype in LMSvc.h and some parameter description here: http://cyberkinetica.homeunix.net/os2tk45/srvfpgr/369_L2_NetServiceControlorN.html . When you load NETAPI32.DLL and use NetServiceControl(NULL, service_name, 3, 0, 0) (3 is for SERVICE_CTRL_UNINSTALL, which is used to stop) the service is stopped afterwards. And it can be deleted and reinstalled afterwards without DeleteFlag or reboot!

So it was never a problem of deleting, but of stopping the service properly. And NetServiceControl() does the trick. Sorry for the long post, but I thought it might help someone with similar problems. (Just for reference, I use Win7 SP1 x64.)

Solution 3

Are you not able to stop the service before the update (and restart after the update) using the commands below?

net stop <service name>
net start <service name>

Whenever I'm testing/deploying a service I'm able to upload files without reinstalling as long as the service is stopped. I'm not sure if the issue you are having is different.

Solution 4

If in .net ( I'm not sure if it works for all windows services)

  • Stop the service (THis may be why you're having a problem.)
  • InstallUtil -u [name of executable]
  • Installutil -i [name of executable]
  • Start the service again...

Unless I'm changing the service's public interface, I often deploy upgraded versions of my services without even unistalling/reinstalling... ALl I do is stop the service, replace the files and restart the service again...

Solution 5

As noted by StingyJack and mcbala, and in reference to comments made by Mike L, my experience is that on a Windows 2000 machine, when uninstalling / reinstalling .Net services, "installutil /u" does require a reboot, even when the service was previously stopped. "sc /delete", on the other hand, does not require a reboot - it deletes the service right away (as long as it is stopped).

I have often wondered, actually, whether there is a good reason "installutil /u" requires a reboot... Is "sc /delete" actually doing something wrong / leaving something hanging?

Share:
114,877

Related videos on Youtube

Nate Sauber
Author by

Nate Sauber

Updated on July 09, 2022

Comments

  • Nate Sauber
    Nate Sauber almost 2 years

    My current project involves deploying an upgraded .exe file that runs as a Windows Service. In order to overwrite the existing .exe with the new version, I currently need to:

    1. Stop the service
    2. Uninstall the service
    3. Reboot the system (so Windows releases it's hold on the file)
    4. Deploy the new .exe
    5. Reinstall the service
    6. Start the upgraded service.

    I'd like to avoid the reboot, so that this can be a fully scripted/automated upgrade.

    Is there any way to avoid rebooting? Maybe a command-line tool that will force Windows to give up it's death grip on the old .exe?

    • Nate Sauber
      Nate Sauber over 15 years
      Thanks everyone for your help! I found that the source of my problem was a handle on the .exe by WMI. See my new question at: stackoverflow.com/questions/302315/…
    • ChandlerPelhams
      ChandlerPelhams almost 12 years
      FYI, if you have the Services manager open, you need to close before re-installing the service or else you will get a 'service is already marked for deletion' error.
    • J0e3gan
      J0e3gan almost 11 years
      In my experience "[SC] CreateService FAILED 1072: The specified service has been marked for deletion" hinges on whether the service being uninstalled has not been stopped first. I tried to reproduce the error simply having the Services administrative tool open and was unable to.
  • Will Dean
    Will Dean over 15 years
    This has always been my experience - I have endlessly updated services merely by stopping them and replacing the .EXE. It's only an ordinary process anyway.
  • Esben Skov Pedersen
    Esben Skov Pedersen over 13 years
    sc delete "service name" < at least on windows 7.
  • StingyJack
    StingyJack over 13 years
    yes... and eventually ms will go to "--" like *nix. One step at a time =)
  • k.honsali
    k.honsali about 11 years
    in my case, the command is sc delete "serviceName" note that you must excute command line as administrator
  • k.honsali
    k.honsali about 11 years
    In my case, I cannot do that due to some permission issue
  • J0e3gan
    J0e3gan almost 11 years
    +1 for the qualification "(as long as it is stopped)". This is consistent with my experience - albeit on Windows 7.
  • J0e3gan
    J0e3gan almost 11 years
    Same here - worked for me as described for months of development on a suite of Windows services (running on Windows 7).
  • J0e3gan
    J0e3gan almost 11 years
    I have found that sc delete does not stop a running service when uninstalling it but rather marks it for deletion, which is what its documentation also describes.
  • shoosh
    shoosh about 9 years
    How do you reliably simulate the "marked for deletion" state? What I did to simulate it is just open a handle to the service with the normal services API. with the handle open, this method does not work. When the handle is closed, the service disappears automatically.
  • Nerielle
    Nerielle almost 8 years
    Thanx! i was looking for the difference between 'net stop' and 'sc stop'
  • Max
    Max about 7 years
    Great analysis!