How to Delete a Configuration Manager Object by Using Managed Code
Applies To: System Center Configuration Manager 2007, System Center Configuration Manager 2007 R2, System Center Configuration Manager 2007 R3, System Center Configuration Manager 2007 SP1, System Center Configuration Manager 2007 SP2
To delete a Microsoft System Center Configuration Manager 2007 object by using the managed SMS Provider, use the IResultObject.Delete method. You can get a IResultObject object for a Configuration Manager 2007 object in numerous ways. For more information, see How to Read a Configuration Manager Object by Using Managed Code
To delete a Configuration Manager object
Set up a connection to the SMS Provider. For more information, see About the SMS Provider in Configuration Manager.
Using the WqlConnectionManager object you obtain in step one, call the GetInstance method to get the IResultObject object for the Configuration Manager object.
Call the IResultObject object Delete method to delete the Configuration Manager object.
Example
The following example deletes a package by using the supplied package identifier. This example uses the WqlConnectionManager class GetInstance method to get an IResultObject object for the Configuration Manager package and then deletes the package.
For information about calling the sample code, see Calling Configuration Manager Code Snippets.
public void DeletePackage(WqlConnectionManager connection, string packageID)
{
try
{
IResultObject package = connection.GetInstance(@"SMS_Package.PackageID='" + packageID + "'");
package.Delete();
}
catch (SmsException ex)
{
Console.WriteLine("Failed to delete package: " + ex.Message);
throw;
}
}
This example method has the following parameters:
Parameter | Type | Description |
---|---|---|
connection |
|
A valid connection to the SMS Provider. |
PackageID |
|
The package identifier for an existing package. This can be obtained from the SMS_Package class PackageID property. |
Compiling the Code
Namespaces
System
System.Collections.Generic
System.ComponentModel
Microsoft.ConfigurationManagement.ManagementProvider
Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine
Assembly
microsoft.configurationmanagement.managementprovider
adminui.wqlqueryengine
Robust Programming
The Configuration Manager exceptions that can be raised are SmsConnectionException and SmsQueryException. These can be caught together with SmsException.
See Also
Concepts
About Configuration Manager Objects
How to Call a Configuration Manager Object Class Method by Using Managed Code
How to Connect to an SMS Provider in Configuration Manager by Using Managed Code
How to Create a Configuration Manager Object by Using Managed Code
How to Modify a Configuration Manager Object by Using Managed Code
How to Perform an Asynchronous Configuration Manager Query by Using Managed Code
How to Perform a Synchronous Configuration Manager Query by Using Managed Code
How to Read a Configuration Manager Object by Using Managed Code
How to Read Lazy Properties by Using Managed Code
How to Use Configuration Manager Objects with Managed Code