Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I'm receiving the following error when the computer object has child objects.

Unable to delete object:

The directory service can perform the requested operation only on a leaf object.


My code:

VB
Shared Function DeleteMachine(ByVal adPath As String, ByVal domain As String) As Boolean
       Try



           Dim ctx As New PrincipalContext(ContextType.Domain, domain, "Domain\Username", "PWD")


           Dim computer As ComputerPrincipal = ComputerPrincipal.FindByIdentity(ctx, adPath)


           If computer IsNot Nothing Then
               computer.Delete()

           Else
               Return False
           End If


           Return True
       Catch ex As Exception
           Return False
       End Try
   End Function


The user has right's do delete an object, if i manually delete the childs i can remove the computer object.

Can someone put some light on this?

Thank you.

[EDIT]

I forgot to mention that the user don't have the AD DeleteTree permission.

The query "CN=MEANMACHINE,OU=Computers,DC=Contoso,DC=com" returns one object, in case the container is empty it deletes the computer, if the container has leafs, it throws the above error.

The path of the container leafs is, for instance, "CN=Virtual Machine,CN=MEANMACHINE,OU=Computers,DC=Contoso,DC=com"

Must i clean all leafs of a container first? How can i know if the container has leafs and how to iterate through them?
Posted
Updated 24-Nov-21 4:22am
Comments
Maciej Los 9-May-12 4:07am    
Are you really sure that query return only one object?
FindByIdentity Exceptions
MultipleMatchesException Multiple computer principal objects matching the current computer object were found.
Try to catch this exception.
LNogueira 9-May-12 6:10am    
Yes. It returns only one object. My adPath is the Distinguish Name of the object.

1 solution

I have found interesting example on http://stackoverflow.com/questions/7910534/how-to-delete-computer-account-from-active-directory-using-c-sharp[^]
The difference is in DeleteTree[^] method for DirectoryEntry, not Delete() for ComputerPrincipal.
Here[^] is a little more about "Container object and Leafs object".

I hope it will be helpful ;)
 
Share this answer
 
Comments
LNogueira 11-May-12 12:10pm    
I know that DeleteTree works. But i was trying to achieve the same without the DeleteTree permission. Maybe iterate over the leafs of a container and delete one by one and then delete the parent container, but i cant found a way to iterate over the leafs of a container...
As it seems the only way to do it, i'm gonna accept your solution as correct.
Thank you
Maciej Los 11-May-12 12:39pm    
I'm afraid that iterate through the leafs collection is not possible. Maybe, the leafs are: (...)"container" objects in the general sense of the word (that is, they can have child objects), but they do not belong to the container class(...)

Have a nice day ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900