Skip to content

Core\Ldap\Connection::delete

$connection->delete(Core\Ldap\Dn|string $dn): void$connection->delete(
    Core\Ldap\Dn|string $dn,
): void

Deletes one entry from the directory. It returns nothing.

You give it the DN of the entry, as a Dn or as text. The connection must log in as an account that may delete the entry.

delete deletes only one entry. An entry that has other entries below it cannot be deleted, and delete throws Core\Ldap\LdapError. Delete the entries below it first, then the entry itself. When there is no entry with the DN, delete throws Core\Ldap\LdapError with $kind set to NoSuchObject.

Good to know: Active Directory keeps a deleted entry for some time, with most of its attributes removed. read and search do not find it, unless the search sets showDeleted.

Parameters

  • $dnCore\Ldap\Dn|stringThe entry's DN, as a Dn or as text. Text cannot be tainted. Build a DN from user input with Dn::of or child.

Return value

Returns void. Nothing.

Errors

  • LogicErrorAn attribute name is not a valid name, a value has no LDAP form, such as a float, or the connection is closed.
  • Core\Ldap\LdapErrorThe server returned an error. $kind says why, such as NoSuchObject, AlreadyExists, InsufficientAccess or ConstraintViolation.

Examples

The examples below run with this configuration file.

nvs.toml
# These examples use the LDAP directory server on this computer at port 16636.
# They log in as the administrator, because they change accounts in the directory.
# `tls_ca_file` is the certificate that the server's certificate is checked against.
[ldap.corp-accounts]
url = "ldaps://127.0.0.1:16636"
base = "DC=example,DC=test"
user = "CN=Administrator,CN=Users,DC=example,DC=test"
password = "Novis-test-1"
tls_ca_file = "../../../../../tests/db/ca.crt"
timeout = "5s"
# The program may open the `corp-accounts` directory.
[capabilities.ldap]
connect = ["corp-accounts"]

Delete a unit

<?nvs
// Shows how to delete an entry from the directory.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');
string $unit = 'OU=Old Projects 1,DC=example,DC=test';
// The program adds the unit first, so it has something to delete.
try {
$corp->add($unit, ['objectClass' => 'organizationalUnit']);
} catch (Core\Ldap\LdapError $exists) {
}
$corp->delete($unit);
// `read` returns `null` for an entry that is not there. This prints `deleted`.
echo $corp->read($unit) == null ? 'deleted' : 'still there', "\n";
Output
deleted

Delete a unit and what is in it

<?nvs
// Shows how to delete a unit that has a user account in it.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');
string $unit = 'OU=Kiosk 2,DC=example,DC=test';
string $person = 'CN=Timo Brandt,' . $unit;
// A run that stopped early can leave the entries behind, so the program deletes them first.
array<string> $leftovers = [$person, $unit];
foreach ($leftovers as string $left) {
try {
$corp->delete($left);
} catch (Core\Ldap\LdapError $missing) {
}
}
$corp->add($unit, ['objectClass' => 'organizationalUnit']);
$corp->add($person, ['objectClass' => 'user', 'sAMAccountName' => 'timo.brandt']);
// The unit still has an entry below it, so `delete` throws an error.
// This prints `the unit is not empty`.
try {
$corp->delete($unit);
} catch (Core\Ldap\LdapError $error) {
echo 'the unit is not empty', "\n";
}
// Delete the entries below a unit first, then the unit. This prints `deleted`.
$corp->delete($person);
$corp->delete($unit);
echo $corp->read($unit) == null ? 'deleted' : 'still there', "\n";
Output
the unit is not empty
deleted

An entry that is not there

<?nvs
// Shows what happens when a program deletes an entry that does not exist.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');
// There is no unit with this DN, so `delete` throws an error.
// This prints `NoSuchObject`.
try {
$corp->delete('OU=Nobody Here 3,DC=example,DC=test');
} catch (Core\Ldap\LdapError $error) {
echo $error->kind == Core\Ldap\LdapError::Kind::NoSuchObject ? 'NoSuchObject' : 'another error', "\n";
}
Output
NoSuchObject