8.5. Deleting Directory Entries

The following excerpt demonstrates how to delete an entry with DN cn=Ted,ou=People,dc=example,dc=com.

final LDAPConnectionFactory factory = new LDAPConnectionFactory(host, port);
Connection connection = null;
try {
    connection = factory.getConnection();
    // Bind as a user who has the right to delete entries.
    connection.bind(adminDN, adminPwd);

    connection.delete("cn=Ted,ou=People,dc=example,dc=com");

} catch (final ErrorResultException e) {
    System.err.println(e.getMessage());
    System.exit(e.getResult().getResultCode().intValue());
    return;
} finally {
    if (connection != null) {
        connection.close();
    }
}

If you must delete an entire branch of entries instead of a single leaf entry, build a DeleteRequest that includes the SubtreeDeleteRequestControl, as described in the section, Subtree Delete Request Control.