Search code examples
pythonrecursionldappython-ldap

Delete recursively a LDAP subtree with python


Using python-ldap, I want to delete an entire subtree of my LDAP tree.

I came up with :

def ldap_recursive_delete_s(con, base_dn):
    search = con.search_s(base_dn, ldap.SCOPE_SUBTREE)
    delete_list = [dn for dn, _ in search]
    delete_list.reverse()

    for dn in delete_list:
        con.delete_s(dn)

I was wondering if there is any kind of "recursive" option like with the ldaprm CLI tool.


Solution

  • i don't think there is any option like that.

    you will have to iterate and delete entries. One gotcha that i would like to mention is you can't delete an OU with objects under it, unless you have a different setting in AD.