Delete method of the Win32_Service class (Remote Desktop Services)

The Delete WMI class method deletes an existing service.

This topic uses Managed Object Format (MOF) syntax. For more information about using this method, see Calling a Method.

Syntax

uint32 Delete();

Parameters

This method has no parameters.

Return value

Returns one of the values listed in the following list or any other value to indicate an error. For additional error codes, see WMI Error Constants or WbemErrorEnum. For general HRESULT values, see System Error Codes.

0

The request was accepted.

1

The request is not supported.

2

The user did not have the necessary access.

3

The service cannot be stopped because other services that are running are dependent on it.

4

The requested control code is not valid, or it is unacceptable to the service.

5

The requested control code cannot be sent to the service because the state of the service (Win32_BaseService.State property) is equal to 0, 1, or 2.

6

The service has not been started.

7

The service did not respond to the start request in a timely fashion.

8

Unknown failure when starting the service.

9

The directory path to the service executable file was not found.

10

The service is already running.

11

The database to add a new service is locked.

12

A dependency this service relies on has been removed from the system.

13

The service failed to find the service needed from a dependent service.

14

The service has been disabled from the system.

15

The service does not have the correct authentication to run on the system.

16

This service is being removed from the system.

17

The service has no execution thread.

18

The service has circular dependencies when it starts.

19

A service is running under the same name.

20

The service name has invalid characters.

21

Invalid parameters have been passed to the service.

22

The account under which this service runs is either invalid or lacks the permissions to run the service.

23

The service exists in the database of services available from the system.

24

The service is currently paused in the system.

Remarks

As your organization changes, you might decide to remove certain services from certain computers. In-house and third-party services can be removed by using WMI, while operating system services can be removed by using Sysocmgr.exe.

When preparing to remove services, keep the following information in mind:

  • Services must be stopped before you remove them. If the service is running when you issue the delete command, the service is marked for deletion, but it continues to run until it stops and all open handles are closed.

    If the service is never stopped, that service will never be deleted.

  • Removing a service does not remove the service's executable file.

    Removing a service by using WMI deletes the related registry entries under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services. As a result, the service is no longer installed and is not available through the Services snap-in. However, WMI does not delete the executable file, meaning you could easily reinstall the service. To delete the executable file, you must retrieve the path name and then delete the file.

  • Removing a base Windows 2000 service (for example, DHCP) by using WMI deletes the registry entries for that service but does not remove the shortcut from the Administrative Tools menu or remove the service from the Windows Components Wizard. This can confuse anyone trying to determine how the computer has been configured.

    For example, if you remove the DHCP service by using a WMI script, the DHCP service is no longer listed in the Services snap-in. However, a nonfunctioning shortcut to the DHCP console remains in the Administrative Tools menu, and if you start the Windows Component Wizard, it indicates that the DHCP service is installed.

    Because of this, you should always use Sysocmgr.exe to programmatically remove Windows 2000 services.

Examples

The following VBScript code sample describes how to delete a service.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Service WHERE Name = 'DbService'")
For Each objService in colListOfServices
 objService.StopService()
 objService.Delete()
Next

The following Perl code sample describes how to delete a service.

use strict;
use Win32::OLE;

my ($Service, $ServiceSet) ;
eval {$ServiceSet = Win32::OLE->GetObject("winmgmts:{impersonationLevel=impersonate}")->
 ExecQuery("SELECT * FROM Win32_Service WHERE Name='MyService'");};
unless($@)
{
 foreach $Service (in $ServiceSet)
 {
  my $RetVal = $Service->Delete();
  if ($RetVal == 0)  
  {
   print "Service deleted \n"; 
  }
  else  
  {
   print "Delete failed: %d", $RetVal;
  }
 }
}
else
{
 print STDERR Win32::OLE->LastError, "\n";
}

Requirements

Requirement Value
Minimum supported client
Windows Vista
Minimum supported server
Windows Server 2008
Namespace
Root\CIMv2\TerminalServices
MOF
TSCfgWmi.mof
DLL
TSCfgWmi.dll

See also

Win32_Service

Operating System Classes

Win32_TerminalService

WMI Tasks: Services