ldapdelete can remove multiple LDAP entries by reading distinguished names (DNs) either from the command line or from a file. This article shows you how to use ldapdelete with a file. This method is commonly used for directory cleanup, migration tasks, and administrative maintenance.
Key points:
The file must contain one DN per line (ldapdelete does not process full LDIF input, so you can't use an LDIF input file format)
LDAP connections can use ldap://, ldaps://, or ldapi:///
Example command:
When deleting multiple entries, you can place the Distinguished Names (DNs) in a file and instruct ldapdelete to delete each entry listed.
Basic Syntax
Example:
Explanation:
Option
Meaning
-x
Use simple authentication
-H
LDAP server URI
-D
Bind DN
-W
Prompt for password
-f
File containing DNs to delete
Creating the Delete File
Create a file containing the DNs you want to remove.
Example filename:
Each line represents one entry to delete.
Deleting Entries Using ldap
Example using a standard ldap connection:
Deleting Entries Using ldaps (Secure LDAP)
If your server requires TLS encryption, use ldaps.
ldaps typically uses port 636
Using Local Administrative Access (ldapi)
Administrators often perform ldap operations locally using the ldapi socket with SASL EXTERNAL authentication:
This method is commonly used for local administrative tasks and does not require a password.
Deleting Entries with Children
LDAP normally prevents deleting entries that contain other entries.
Example error:
To delete an entry and all of its children, use the -r option.
Recursively delete entries below a DN. Warning — use carefully!
Testing Before Deleting
You can perform a dry run before deleting entries, where you can see what the output would be, had the deletions been performed:
Options used:
Option
Purpose
-n
Do not execute deletes
-v
Verbose output
Example output:
Because the “-n” option was used, these entries will NOT be deleted.
Continue if Errors Occur
If deleting many entries, you may want the command to continue even if one delete fails:
The -c option tells ldapdelete to continue processing even if an error occurs.
Recommended Workflow
A safe workflow when deleting entries is:
Identify entries using ldapsearch.
Copy the DNs into a file.
Test with ldapdelete -n.
Run the delete command.