197

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)?

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Manu
  • 28,753
  • 28
  • 75
  • 83
  • If the service is hanging and deletion does not work see [How to force uninstallation of windows service](https://stackoverflow.com/a/6292700/3027266) – Wernfried Domscheit Jan 28 '20 at 12:16

20 Answers20

534

One thing that has caught me out in the past is that if you have the services viewer running then that prevents the services from being fully deleted, so close that beforehand

the_mandrill
  • 29,792
  • 6
  • 64
  • 93
  • 88
    I can hardly believe that having the viewer open breaks service removal - how stupid! – Draemon Nov 07 '12 at 23:21
  • If this is on a server (with remote desktop connections), you might also need to close all consoles (mmc.exe) that may be running in other sessions. – robasta Jun 26 '13 at 11:44
  • 1
    This is so stupid from Microsoft, but it works indeed. For some unexplainable reason, having the services viewer open prevents them from being properly deleted sometimes. – ivantod Dec 12 '13 at 13:50
  • @ivantod - it appears to lock the process associated with the service. Dumb as hell. – arviman Sep 16 '14 at 06:52
  • Worked for me on Windows Server 2012. I can't believe the viewer can lock a service like that. – Robin Jan 22 '15 at 13:49
  • 1
    I think that this answer is gonna have hundred of similar comments. I've just lost 30min with this stupid issue ... – pierroz Mar 27 '15 at 10:11
  • 3
    It seems that SysInternals process explorer also locks the services (and maybe also task manager) so make sure you close those too! – danio Aug 27 '15 at 13:05
  • Didn't work for me. I cannot remove the service in any way. – bashan Aug 29 '15 at 12:08
  • +1, this worked for me. In my case, someone else had an RDP session open on the server and he had the service viewer opened. I had to shutdown his session and then the service was deleted. – spetry Dec 17 '15 at 07:57
  • 3
    also make sure to use cmd prompt. PowerShell does not show any error, but doesn`t delete either. – Bruno Faria Feb 11 '16 at 20:01
  • THANK YOU. This was driving me nuts. – joshschreuder Dec 14 '16 at 01:06
  • 1
    Good job, Microsoft – kv1dr Jan 31 '17 at 11:44
  • It doesn't work for me with VS2015 windows installer package. Also this is not appropriate solution. Hence, I down vote. – RJN Jun 15 '17 at 11:53
  • 1
    A classic Microsoft feature... Didn't expected that I just have to close the services UI.. Big thatks for the help! – Chris S. Aug 22 '17 at 06:25
  • Windows Vista removed the service database lock; this may have been a cause in the past (the answer is from 2009) but it probably won't matter for Windows 7+ – MSalters Jun 29 '18 at 13:24
  • Unreal that this is a thing... saved me some time. – Jeffrey Van Laethem Aug 08 '18 at 17:15
  • It works as a charm with one service and Services Viewer running and it doesn't work with *another* service, which is actually a ***copy*** of the first! – Oak_3260548 Nov 01 '21 at 08:02
160

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.

dragon788
  • 3,583
  • 1
  • 40
  • 49
johan
  • 6,578
  • 6
  • 46
  • 68
  • This is a great answer. Much better than a reboot as suggested in the less good (IMHO) accepted answer. – Benedict Nov 09 '11 at 16:04
  • 41
    This didn't work for me - the pid for my service returned 0 and it wouldn't let me delete a critical system process. – plasma147 Jul 31 '12 at 08:40
  • 4
    A+ awesome. Use sc delete then the above. – Zone12 Jan 04 '13 at 06:21
  • 10
    didn't worked as PID came as 0, this solved my issue - [(SC) DeleteService FAILED 1072](http://stackoverflow.com/a/7202888) – Web-E Nov 26 '13 at 06:26
  • 3
    Absolutely helpful! In my case, nothing short of a reboot worked. Not even deleting the service directory in Registry! One gotcha though: If you're working in PowerShell use: `sc.exe queryex `. Because `sc` is also an alias for `Set-Content`, it will SILENTLY fail when you use that. – Ifedi Okonkwo Nov 28 '14 at 07:17
  • OP asks how to delete a service when `service is marked for deletion`. In this case the service is not running any more and therefore there is no PID and no process which can be killed. Your solution helps when the process is still running but cannot be stopped. (But: When the service is still running and is hosted on a svchost, then you will kill also all other processes which use this svchost.) – marsh-wiggle May 24 '21 at 09:16
108

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

  • 5
    Thanks for this. Note that if the "Service name" is not the same as the "Display name", you can get the "Service name" in the service's properties. – GuiSim Dec 09 '11 at 15:16
  • 9
    `sc delete` will also report "service is marked for deletion" if there is a handle open to the service. – Matt Nov 16 '13 at 13:36
  • @GuiSim thanks for that, I suspect many will be putting the display name like I was! – nik0lai Oct 20 '16 at 14:51
  • Tried that but got this message: [SC] DeleteService FAILED 1072: The specified service has been marked for deletion. – Andrew W. Phillips Jan 23 '19 at 02:37
  • If the service name has space in between then we need to mention the service name using double quotes. For example: sc delete "Service Name" – Srini Karthikeyan Apr 01 '21 at 09:33
40

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.

Martin
  • 1,355
  • 2
  • 14
  • 21
13
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.

Aaron Weiker
  • 2,523
  • 2
  • 21
  • 22
  • Just an addition.. I had to run my dos prompt/.NET prompt as administrator to gain access to run the sc delete command.. just in case anyone else runs into that issue. – Dav.id May 14 '12 at 11:56
  • Run "NET STOP " prior to SC DELETE to ensure the service was stopped before deleting – Adi Sep 06 '12 at 11:50
  • This was my experience as well, I eventually had to reboot to remove a stubborn service that was only partially uninstalled (it still showed up in the snap in with a code 2). Rebooting was what finally made it go away. – delliottg Oct 04 '16 at 16:09
9

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"
Richard
  • 3,207
  • 5
  • 30
  • 35
  • You saved me. I was doing sc delete on powershell – mandy1339 Dec 07 '21 at 00:53
  • Couldnt stop, pause, end te service in Services.msc even with Admin Rights. Hence from there I could also not delete it. via CMD in Admin mode using this command, it worked like a charm, thanks! – tim Dec 15 '22 at 11:54
  • nice, thanks. PS just does not say jack when running the sc command, so I was under the impression that it should have worked, which it did not. Thanks a lot! – modmoto Jun 27 '23 at 10:36
6

This worked for me

$a = Get-WmiObject Win32_Service | Where-Object {$_.Name -eq 'psexesvc'}
$a.Delete()
expirat001
  • 2,125
  • 4
  • 30
  • 40
  • If your output from that shows `ReturnValue : 16`, then the command failed with "_This service is being removed from the system_". Full list of the ReturnValues from Microsoft [here](https://msdn.microsoft.com/en-us/library/aa393673(v=vs.85).aspx) – Signal15 Mar 09 '17 at 18:16
6

This worked for me:

net stop "MyWindowsService"
taskkill /F /IM mmc.exe
sc delete "MyWindowsService"
Alper Ebicoglu
  • 8,884
  • 1
  • 49
  • 55
5

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

Necoras
  • 6,743
  • 3
  • 24
  • 45
incomplete
  • 548
  • 1
  • 7
  • 12
4

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

seanyboy
  • 5,623
  • 7
  • 43
  • 56
3

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.

Community
  • 1
  • 1
Tan
  • 221
  • 1
  • 7
3

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.

  • On Windows 2012 R2, causes the Services MMC snap-in (Services.MSC) to display "_Failed to Read Description. Error Code: 2_". Despite ensuring all other MMCs were closed and no other users logged-in. – Signal15 Mar 09 '17 at 18:21
  • I see, only tested in Windows 8 & Windows 10 (and it worked). – Fernando Gonzalez Sanchez Mar 10 '17 at 02:29
  • THAT finally solved my problem. I forgot to add a service installer in the project and could not remove my service after the installation. I recommend doing a export of the service reg tree before deleting anything in it. Thanks a lot! – user3772108 Aug 01 '19 at 07:20
2

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
Dmitry
  • 299
  • 1
  • 10
1

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).

marco
  • 685
  • 4
  • 18
1

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

faulty
  • 8,117
  • 12
  • 44
  • 61
1

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.

Oguzhan Kircali
  • 453
  • 5
  • 11
0

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.

Unsliced
  • 10,404
  • 8
  • 51
  • 81
0

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!

Jer
  • 1
0

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
  • 2
    Where do you get the handle to close if your not in the program that has the open handle? – Matt Nov 16 '13 at 13:32
0

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
dragon788
  • 3,583
  • 1
  • 40
  • 49