Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 32 lines (26 sloc) 1.022 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#!/usr/bin/perl -w
#
# recursive-ldap-delete.pl
#
# originally by Mike Jackson <mj@sci.fi>
# shortened by Peter Marschall <peter@adpm.de>
# based on ideas by Norbert Kiesel <nkiesel@tbdetworks.com>
#
# ToDo: check errors, handle references, ....

use strict;
use Net::LDAP;

my $server = "localhost";
my $binddn = "cn=directory manager";
my $bindpasswd = "foobar";
my $delbranch = "ou=users,dc=bigcorp,dc=com"; # branch to remove

my $ldap = Net::LDAP->new( $server ) or die "$@";
$ldap->bind( $binddn, password => $bindpasswd, version => 3 );

my $search = $ldap->search( base => $delbranch,
                                 filter => "(objectclass=*)" );

# delete the entries found in a sorted way:
# those with more "," (= more elements) in their DN, which are deeper in the DIT, first
# trick for the sorting: tr/,// returns number of , (see perlfaq4 for details)
foreach my $e (sort { $b->dn =~ tr/,// <=> $a->dn =~ tr/,// } $search->entries()) {
  $ldap->delete($e);
}

$ldap->unbind();
Something went wrong with that request. Please try again.