Managing the lifecycle of entries after they're created: the add, delete, and replace operations on attributes, the changetype directive, renaming RDNs with modrdn, moving entries with newsuperior, deleting entries and their subtrees, plus backup and validation practices.

In episode 7 you populated the directory with users and groups. Episode 8 covers the other side of the data lifecycle: changing attributes, adding new values, removing attributes, renaming entries, moving entries between parents, and deleting entries — all through ldapmodify, ldapmodrdn, and ldapdelete. This is the episode where the data you've entered starts to move and change.
ldapmodify reads LDIF files containing the changetype directive. Four values are available:
| changetype | Function | Equivalent tool |
|---|---|---|
add | Adds a new entry | ldapadd |
delete | Deletes an entry | ldapdelete |
modify | Changes an entry's attributes | ldapmodify |
modrdn | Renames or moves an entry | ldapmodrdn |
Episode 7 used changetype: add implicitly through ldapadd. Now we focus on modify and modrdn.
There are three attribute-change operations:
| Operation | Effect |
|---|---|
add | Adds a new attribute, or a new value to a multi-valued attribute |
delete | Removes an attribute along with all its values, or one specific value |
replace | Replaces all of an attribute's values with new ones |
In an LDIF file, each operation block ends with a single dash line:
dn: uid=budi,ou=People,dc=example,dc=com
changetype: modify
add: mail
mail: budi.santoso@example.com
-
replace: description
description: Budi's primary account
-
delete: telephoneNumberRun it with ldapmodify:
ldapmodify -x -H ldap://localhost -D "cn=admin,dc=example,dc=com" -W -f ubah-budi.ldifThe changetype: modify directive marks this as a modify operation. The add: followed by mail: block adds the mail attribute; replace: replaces the value of description; delete: removes the telephoneNumber attribute. For multi-valued attributes, you can specify the specific value you want to remove:
dn: cn=devel,ou=Groups,dc=example,dc=com
changetype: modify
delete: member
member: uid=aji,ou=People,dc=example,dc=comldapmodify processes many operations at once in a single file. Each entry carries its own changetype, and each operation block still ends with a single dash:
dn: uid=sari,ou=People,dc=example,dc=com
changetype: modify
add: telephoneNumber
telephoneNumber: +62 21 555 0101
dn: uid=aji,ou=People,dc=example,dc=com
changetype: modify
replace: loginShell
loginShell: /bin/zshOne file can mix modify, add, even delete for different entries. As in episode 7, use -c so processing continues even if an operation fails, and check the return code in scripts.
The ModifyDN operation changes an entry's Relative Distinguished Name (RDN) and/or moves it to a new parent. There are two tools: ldapmodrdn for command-line form, or ldapmodify with changetype: modrdn.
dn: uid=budi,ou=People,dc=example,dc=com
changetype: modrdn
newrdn: uid=budi.santoso
deleteoldrdn: 0newrdn specifies the new RDN; deleteoldrdn: 1 removes the old RDN attribute, while 0 keeps it as an ordinary attribute. Run it:
ldapmodify -x -D "cn=admin,dc=example,dc=com" -W -f rename-rdn.ldifThe command-line form uses ldapmodrdn:
ldapmodrdn -x -D "cn=admin,dc=example,dc=com" -W -r "uid=budi,ou=People,dc=example,dc=com" "uid=budi.santoso"The -r option removes the old RDN — equivalent to deleteoldrdn: 1.
The newsuperior directive moves an entry under a new parent:
dn: uid=budi,ou=People,dc=example,dc=com
changetype: modrdn
newrdn: uid=budi
deleteoldrdn: 0
newsuperior: ou=Contractors,dc=example,dc=comAfter this operation, the entry sits at uid=budi,ou=Contractors,dc=example,dc=com. Note that newsuperior is only available on servers that support it and requires appropriate access rights — usually admin only.
ldapdelete accepts one or several DNs, or a list of DNs from a file with -f:
ldapdelete -x -D "cn=admin,dc=example,dc=com" -W "uid=test,ou=People,dc=example,dc=com"The LDAP protocol doesn't define a delete-subtree operation. Deleting an entry that still has children is rejected by the server with code 66 notAllowedOnNonLeaf. So to delete a subtree, delete from the leaves up to the root:
ldapdelete -x -D "cn=admin,dc=example,dc=com" -W "uid=test,ou=People,dc=example,dc=com"
ldapdelete -x -D "cn=admin,dc=example,dc=com" -W "ou=People,dc=example,dc=com"The bottom-up delete loop can be written in a shell script with the help of ldapsearch to list child DNs first.
When a user is deleted, the DN references in a group's member attribute aren't removed automatically — this is what's called a dangling reference. The impact: the group still lists members who no longer exist. The solution is the refint overlay (episode 11) or manual cleanup:
dn: cn=devel,ou=Groups,dc=example,dc=com
changetype: modify
delete: member
member: uid=aji,ou=People,dc=example,dc=comslapcat to back up the database contents (full discussion in the backup and restore episode).ldapsearch:ldapsearch -x -D "cn=admin,dc=example,dc=com" -W -b "uid=budi,ou=People,dc=example,dc=com" -s base "(objectClass=*)"auditlog overlay (episode 11) to record who changed what.Warning
The modrdn and ldapmodrdn operations are sensitive: changing an RDN changes the DN of the entire subtree below it, so references in other attributes break too. Make sure the refint overlay is active, or check the references pointing to that entry first, before renaming.
Episode 8 completes the entry lifecycle: the three modification operations add, delete, replace; the changetype directive including modify and modrdn; renaming RDNs with newrdn; moving entries with newsuperior; deleting entries and their subtrees; and the practice of keeping references clean.
Key takeaways:
ldapmodify operation block ends with a single dash.deleteoldrdn determines whether the old RDN attribute is also removed.In the next episode, episode 9, you'll sharpen your reading skills: search scopes, filters like wildcards and comparisons, logical operators, attribute selection techniques, and keeping search performance in check.