Skip to content

Delete

For the complete documentation index see: llms.txt

All documentation pages available in markdown.

Jump to the Code block for a combined complete example.

Description

Demonstrates single record deleletions in an Aerospike database.

Setup

Boilerplate

The examples below leverage the following boilerplate to initialize the Aerospike module and connect to a server before deleting a record.

const Aerospike = await import("aerospike");
// Set hosts to your server's address and port
const config = { hosts: "YOUR_HOST_ADDRESS:YOUR_PORT" };
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001
const key = new Aerospike.Key("sandbox", "ufodata", 5001);
// Establishes a connection to the server
const client = await Aerospike.connect(config);

Policies

Just as Create and Update, Delete utilizes write policies that can be defined per command.

The following example creates a write policy that enables durable deletes.

// Can use a previously defined write policy or create a new one
const deletePolicy = new Aerospike.WritePolicy({
durableDelete: true,
});

Delete a record

Default policy

Aerospike deletes a record efficiently by only dropping the primary index entry. The record space on device is recovered during garbage collection.

The following example shows how to delete a record using the default write policy.

// Delete record with default write policy
await client.remove(key);

If no record exists for the key, remove() rejects with an AerospikeError whose code is Aerospike.status.ERR_RECORD_NOT_FOUND. The following example treats a record that is already absent as deleted, and rethrows any other error.

try {
await client.remove(key);
} catch (error) {
if (error.code !== Aerospike.status.ERR_RECORD_NOT_FOUND) {
throw error;
}
console.info("Record already deleted");
}

Durable deletes

When a command results in deletion of a record and the durable delete write policy is set to true, a tombstone record is written to disk to prevent previous versions of the record from returning during a cold restart. Refer to Durable Deletes for more information.

The following example uses the write policy set above to durably delete a record.

// Durably delete a record with the write policy
await client.remove(key, deletePolicy);

Delete every record in a set

client.truncate() deletes every record in a set far faster than deleting the records one at a time. The following example deletes every record in the ufodata set.

// Delete every record in the ufodata set
await client.truncate("sandbox", "ufodata", 0);

The arguments are the namespace, the set, and a cutoff time. The cutoff is a last-update time in nanoseconds since the Unix epoch. Only records last updated before it are deleted. 0 sets the cutoff to the time the server receives the command.

After the promise resolves, reads and queries no longer return the truncated records. Records written after the truncate are kept. The server compares last-update times to the millisecond, so a record written in the same millisecond as the truncate can survive it.

In Enterprise Edition, truncation is durable through a cold restart. In Community Edition, truncated records can return after a cold restart. See Sets.

Code block

Expand this section for a single code block to durably delete a record
const Aerospike = await import("aerospike");
// Set hosts to your server's address and port
const config = { hosts: "YOUR_HOST_ADDRESS:YOUR_PORT" };
// Establishes a connection to the server
const client = await Aerospike.connect(config);
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001
const key = new Aerospike.Key("sandbox", "ufodata", 5001);
// Can use a previously defined write policy or create a new one
const deletePolicy = new Aerospike.WritePolicy({
durableDelete: true,
});
// Durably delete a record with the write policy
await client.remove(key, deletePolicy);
// Close the connection to the server
await client.close();