Deleting LDAP entry using JNDI

In the previous chapter we have seen how to add a new entry to an LDAP directory using JNDI. There we added a new user record to existing directory. It is often needed in Java applications to delete an entry from the LDAP directory. By using JNDI we can do the deletion.Here we are discussing how we can do   Deleting LDAP entry using JNDI .

Deleting LDAP entry using JNDI

In the previous example we added  a new user record to the directory. In the example we are discussing here deletes the same record we added there.

import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;

public class EntryDeleteSample {
public EntryDeleteSample() {


public void deleteEntry() {
Properties initilaProperties = new Properties();
initilaProperties.put(Context.PROVIDER_URL, "ldap://localhost:10389");
.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
initilaProperties.put(Context.SECURITY_CREDENTIALS, "secret");

try {
DirContext context = new InitialDirContext(initilaProperties);
System.out.println("Existing users before deletion....");
.println("Deletion of user with employeeNumber = 333333 starts");
System.out.println("After deletion");

} catch (NamingException n) {

public void deleteUser(DirContext context) {
try {
} catch (NamingException e) {

public void listEntries(DirContext context) {
String searchFilter = "(objectClass=inetOrgPerson)";
String[] requiredAttributes = { "employeeNumber", "cn",
"telephoneNumber" };

SearchControls controls = new SearchControls();

NamingEnumeration users;
try {
users = context
.search("ou=users,ou=system", searchFilter, controls);

SearchResult searchResult = null;
String commonName = null;
String empNumber = null;
String telephoneNumber = null;
while (users.hasMore()) {

searchResult = (SearchResult);
Attributes attr = searchResult.getAttributes();
commonName = attr.get("cn").get(0).toString();
empNumber = attr.get("employeeNumber").get(0).toString();
telephoneNumber = attr.get("telephoneNumber").get(0).toString();
System.out.println("Name = " + commonName);
System.out.println("Employee Number = " + empNumber);
System.out.println("Phone Number = " + telephoneNumber);

} catch (NamingException e) {
public static void main(String[] args) {
EntryDeleteSample sample = new EntryDeleteSample();

Compile and run the code. I am giving the output I got.


Existing users before deletion….

Name = Bijoy

Employee Number = 112233

Phone Number = 99999999999

Name = users

Employee Number = 333333

Phone Number = 777777777

Deletion of user with employeeNumber = 333333 starts

After deletion

Name = Bijoy

Employee Number = 112233

Phone Number = 99999999999

See Related Posts:

JNDI overview

Naming Service Example using JBossNS

Accessing LDAP using JNDI

Updating  attribute of an LDAP entry using JNDI

Deleting attribute of an LDAP entry using JNDI

Adding attribute to an existing LDAP entry

Displaying user entries in LDAP using JNDI

Adding LDAP entry using JNDI

One thought on “Deleting LDAP entry using JNDI

  1. abducar1 Reply

    Thanks for the above code. nice sharing.
    I have a question.
    I use my mailforwardingaddress have multiple defined users in the LDAP system.
    I want to list the addresses of these users mailforwardingaddress.
    I also want to delete a specific mailforwardingaddress value.
    How can I do?
    Can you help?

Leave a Reply

Your email address will not be published. Required fields are marked *