{"id":69393,"date":"2005-07-14T22:08:00","date_gmt":"2005-07-14T22:08:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/07\/14\/how-can-i-delete-an-active-directory-object-that-contains-other-objects\/"},"modified":"2005-07-14T22:08:00","modified_gmt":"2005-07-14T22:08:00","slug":"how-can-i-delete-an-active-directory-object-that-contains-other-objects","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-delete-an-active-directory-object-that-contains-other-objects\/","title":{"rendered":"How Can I Delete an Active Directory Object that Contains Other Objects?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I delete an Active Directory object that contains other objects?<BR><BR>&#8212; TS<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, TS. Ah, yes, the dreaded \u201cobjects that contain other objects\u201d dilemma. Is there a way to delete an Active Directory object that contains other objects? Let\u2019s see if we can find one.<\/P>\n<P>For those of you new to Active Directory scripting, deleting an object (a user account, a computer account, whatever) typically takes just two lines of code. For example, this little script deletes an object from Active Directory; it simply binds to the domain root (dc=fabrikam, dc=com) and then uses the <B>Delete<\/B> method to delete an OU (organizationalUnit) named Finance:<\/P><PRE class=\"codeSample\">Set objDomain = GetObject(&#8220;LDAP:\/\/dc=fabrikam,dc=com&#8221;)\nobjDomain.Delete &#8220;organizationalUnit&#8221;, &#8220;ou=Finance&#8221;\n<\/PRE>\n<P>Well, check that: it deletes an OU named Finance <I>provided<\/I> that the OU is empty; that is, the OU will get deleted as long as it doesn\u2019t contain any other objects. If Finance does contain other objects (for example, a bunch of user accounts) then the OU will not be deleted; instead, you\u2019ll get this somewhat cryptic error message:<\/P><PRE class=\"codeSample\">An invalid dn syntax has been specified.\n<\/PRE>\n<P>Why is the syntax invalid? Well, the Finance OU is a container (an object that can contain other objects), and the Delete method cannot delete a container unless that container is empty. Because the Finance OU isn\u2019t empty it\u2019s not valid the call the Delete method. As a result, the syntax is invalid, and the script fails.<\/P>\n<P>So what do we do about that? Well, we could get a list of all the objects stored in the Finance OU, delete each of those objects one-by-one, and <I>then<\/I> delete the Finance OU. Or we could use this script instead:<\/P><PRE class=\"codeSample\">Set objOU = GetObject(&#8220;LDAP:\/\/OU=finance,DC=fabrikam,DC=com&#8221;)\nobjOU.DeleteObject(0)\n<\/PRE>\n<P>Yes, it looks very similar, but there are a couple differences. To begin with, we bind directly to the Finance OU itself; in our previous script we bound to the parent object (in that case, the domain root). Second, we don\u2019t use the Delete method; instead, we use the <B>DeleteObject<\/B> method. Unlike its counterpart, DeleteObject will delete an object regardless of whether or not it contains other objects. In one fell swoop, the Finance OU and everything in it will be deleted.<\/P>\n<P>Note, too that we passed DeleteObject a single parameter: 0. This is a required parameter; leave it out and the call to DeleteObject will fail. Interestingly enough, however, 0 is the only possible value that can be passed to DeleteObject; there are no options other than 0, which means, \u201cGo ahead and delete everything.\u201d Why? To tell you the truth we don\u2019t know, just like we don\u2019t know for sure why there are two methods (Delete and DeleteObject) for deleting things from Active Directory. But you know what they say: when it comes to certain things &#8211; like what they put into hot dogs and how they delete objects from Active Directory &#8211; well, maybe you\u2019re better off <I>not<\/I> knowing. Just use DeleteObject and delete away.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I delete an Active Directory object that contains other objects?&#8212; TS Hey, TS. Ah, yes, the dreaded \u201cobjects that contain other objects\u201d dilemma. Is there a way to delete an Active Directory object that contains other objects? Let\u2019s see if we can find one. For those of you new to [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[7,9,3,4,5],"class_list":["post-69393","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-adsi","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I delete an Active Directory object that contains other objects?&#8212; TS Hey, TS. Ah, yes, the dreaded \u201cobjects that contain other objects\u201d dilemma. Is there a way to delete an Active Directory object that contains other objects? Let\u2019s see if we can find one. For those of you new to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69393","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=69393"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69393\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=69393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}