How to Delete a Configuration Manager Object by Using Managed Code
To delete a Configuration Manager object by using the managed SMS Provider, use the IResultObject.Delete method. You can get a IResultObject object for a Configuration Manager 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 SMS Provider fundamentals.
Using the
WqlConnectionManager
object you obtain in step one, call theGetInstance
method to get theIResultObject
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 |
- WqlConnectionManager | A valid connection to the SMS Provider. |
PackageID |
- String |
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
Objects overview
How to Call a Configuration Manager Object Class Method by Using Managed Code
How to Connect to a Configuration Manager Provider 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