Directory Services

IADsContainer::Delete

The IADsContainer::Delete method deletes a specified directory object from this container.

HRESULT Delete( 
  BSTR bstrClass,
  BSTR bstrRelativeName
);

Parameters

bstrClass
[in] The schema class object to delete. The name is that returned from the IADs::get_Class method. Also, NULL is a valid option for this parameter. Providing NULL for this parameter is the only way to deal with defuncted schema classes. If an instance was created before the class is defuncted, the only way to delete the instance of the defuncted class is to call IADsContainer::Delete and provide NULL for this parameter.
bstrRelativeName
[in] Name of the object as it is known in the underlying directory and identical to the name retrieved with the IADs::get_Name method.

Return Values

This method supports the standard return values, including S_OK for a successful operation. For more information about error codes, see ADSI Error Codes.

Remarks

The object to be deleted must be a leaf object or a childless subcontainer. To delete a container and its children, that is, a subtree, use IADsDeleteOps::DeleteObject.

The specified object is immediately removed after calling IADsContainer::Delete and calling IADs::SetInfo on the container object is unnecessary.

When using the IADsContainer::Delete method to delete an object in C/C++ applications, release the interface pointers to that object as well. This is because the method removes the object from the underlying directory immediately, but leave intact any interface pointers held, in memory, by the application, for the deleted object. If not released, confusion can occur in that you may call IADs::Get and IADs::Put on the deleted object without error, but will receive an error when you call IADs::SetInfo or IADs::GetInfo on the deleted object.

Example Code [Visual Basic]

The following code example deletes a user object from the container in Active Directory.

Dim cont as IADsContainer
On Error GoTo Cleanup

Set cont = GetObject("LDAP://OU=Sales,DC=Fabrikam,DC=com")
cont.Delete "user", "CN=JeffSmith"

Cleanup:
	If (Err.Number<>0) Then
		MsgBox("An error has occurred. " & Err.Number)
	End If
	Set cont = Nothing

The following code example deletes a user object from the container under WinNT provider.

Dim cont as IADsContainer
On Error GoTo Cleanup

Set cont = GetObject("WinNT://Fabrikam")
cont.Delete "user", "jeffsmith"

Cleanup:
	If (Err.Number<>0) Then
		MsgBox("An error has occurred. " & Err.Number)
	End If
	Set cont = Nothing

Example Code [C++]

The following code example deletes a user using IADsContainer::Delete.

HRESULT hr = S_OK;
IADsContainer *pCont=NULL;
 
CoInitialize(NULL);
 
hr = ADsGetObject(L"WinNT://myMachine", 
				IID_IADsContainer, 
				(void**) &pCont);
if ( !SUCCEEDED(hr) )
{
	 return hr;
}
 
hr = pCont->Delete(CComBSTR("user"), CComBSTR("JeffSmith"));
pCont->Release();

Requirements

Client: Included in Windows XP and Windows 2000 Professional.
Server: Included in Windows Server 2003 and Windows 2000 Server.
Redistributable: Requires Active Directory Client Extension on Windows NT 4.0 SP6a and Windows 95/98/Me.
Header: Declared in Iads.h.

See Also

ADSI Error Codes, IADs::Get, IADs::GetInfo, IADs::get_Class, IADs::get_Name, IADs::Put, IADs::SetInfo, IADsContainer, IADsContainer::Create, IADsDeleteOps::DeleteObject