Questions

Forum Navigation
Please to create posts and topics.

Microsoft Windows - "The specified service has been marked for deletion" error

Hello,

I'm in a scenario in which, while preparing some automation scripts on several Windows Server systems, while trying deleting a service (services.msc), I get the following error message:

The specified service has been marked for deletion

How to solve this issue?

There may be several causes which lead to the service being stuck in marked for deletion status. The following scenarios are the most common:

  • SysInternals' Process Explorer is opened. Closing it should lead to automatic removal of the service.
  • Task Manager is opened (less common scenario).
  • Microsoft Management Console (MMC) is opened. To ensure all instances are closed, run taskkill /F /IM mmc.exe.
  • Services console is opened. This is the same as the previous point, since Services console is hosted by MMC. Close the services.msc console or kill the mmc.exe process as shown in the previous point.
  • Event Viewer is opened. Again, this is the same as the third point.
  • The key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\{service name} exists.
  • Someone else is logged into the server and has one of the previously mentioned applications opened. Lof off all the Disconnected users.
  • An instance of Visual Studio is used to debug the service is actually open.

The general approach, specially in automation, scripting or deployments, is to identify and kill the process, using PID or image name, running that service using taskkill in Windows systems.

If you want to use the PID approach it could be usefult to use, for example, sc queryex {service name} and then kill process with taskkill /F /PID {Service PID}.

You can stop a hung service more elegantly without manually checking the PID of the service process. The taskkill tool has the /FI option, which allows you to use a filter to select the necessary services or processes. You can kill a specific service as follows:

taskkill /F /FI "SERVICES eq SysMain"

Or you can skip the service name at all and killing all services in a hung state with the command:

taskkill /F /FI "status eq not responding"

After that, the service that is stack in the Stopping status should stop.

You can also use the taskkill utility to force stop the hang services on a remote computer:

taskkill /S mun-fs01 /F /FI "SERVICES eq SysMain"

Another quick way, using scripting code, to stop processes of services in a "Stop Pending" status is by querying the services in such status with Get-WmiObject (Windows Management Instrumentation) cmdlet and, for each of them, passing the PID of the respective service's process to the Stop-Process cmdlet to kill it.

$Services = Get-WmiObject -Class win32_service -Filter "state = 'stop pending'"
if ($Services) {
   foreach ($service in $Services) {
      try {
          Stop-Process -Id $service.processid -Force -PassThru -ErrorAction Stop
      }
      catch {
          Write-Warning -Message "Error. Error details: $_.Exception.Message"
      }
   }
}
else {
    Write-Output "No services with 'Stopping'.status"
}