Permalink
Browse files

Add example script for recursive deletes

  • Loading branch information...
1 parent 4ed97d0 commit 3477ef1635ae200889deb29c315567a816e53138 @gbarr gbarr committed Sep 1, 2004
Showing with 44 additions and 0 deletions.
  1. +1 −0 MANIFEST
  2. +43 −0 contrib/recursive-ldap-delete.pl
View
@@ -19,6 +19,7 @@ contrib/ldifdiff.pl
contrib/ldifsort.pl
contrib/ldifuniq.pl
contrib/printMembers.pl
+contrib/recursive-ldap-delete.pl
contrib/tklkup
data/00-cmp.ldif
data/00-cmp2.ldif
@@ -0,0 +1,43 @@
+#!/usr/bin/perl -w
+#
+# recursive-ldap-delete.pl
+#
+# Mike Jackson <mj@sci.fi>
+#
+
+use strict;
+use Net::LDAP;
+
+my $server = "localhost";
+my $binddn = "cn=directory manager";
+my $bindpasswd = "foobar";
+my $base = "dc=bigcorp,dc=com";
+my $delbranch = "ou=users,$base"; # branch to remove
+
+my $ldap = Net::LDAP->new( $server ) or die "$@";
+$ldap->bind( $binddn, password => $bindpasswd, version => 3 );
+my $result = $ldap->search( base => $delbranch,
+ filter => "(objectclass=*)" );
+
+my @dnlist;
+my $entry;
+foreach $entry ( $result->all_entries ) { push @dnlist, $entry->dn }
+
+# explode dn into an array and push
+# arrays to indexed hash of arrays
+my %HoL;
+my $i = 0;
+for ( @dnlist ) {
+ s/,$base//;
+ $HoL{$i} = [ split(",", $_) ];
+ $i++;
+}
+
+# sorted descending by number of members (leaf nodes last)
+foreach my $key ( sort { @{$HoL{$b}} <=> @{$HoL{$a}} } keys %HoL ) {
+ my $dn = join(",", @{ $HoL{$key} }).",$base";
+ $ldap->delete($dn);
+}
+
+$entry->update ( $ldap );
+$ldap->unbind;

0 comments on commit 3477ef1

Please sign in to comment.