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.
Setup
The following examples will use the setup below to illustrate single record deletion in an Aerospike database.
using Aerospike.Client;
// Define host configurationHost config = new Host("127.0.0.1", 3000);// Establishes a connection to the serverAerospikeClient client = new AerospikeClient(null, config);
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001Key key = new Key("sandbox", "ufodata", 5001);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 oneWritePolicy deletePolicy = new WritePolicy();deletePolicy.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 policyclient.Delete(null, key);
// Close the connection to the serverclient.Close();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 policyclient.Delete(deletePolicy, key);
// Close the connection to the serverclient.Close();Delete every record in a set
To delete all the records in a set with one server-side call, use AerospikeClient.Truncate. It is much faster than deleting records one at a time.
Unlike Delete, it takes an InfoPolicy, a namespace, and a set name instead of a key. Pass null for the policy to use the default. The last argument is an optional cutoff, DateTime?, and null deletes every record in the set:
// Delete every record in the "ufodata" setclient.Truncate(null, "sandbox", "ufodata", null);📖 API reference:
AerospikeClient|InfoPolicy
Truncate returns no value. It doesn’t report how many records it deleted.
To delete only the records that were last updated before a given time, pass that time as the last argument. The value must be earlier than the current time. Records updated at or after the cutoff stay. The server compares the cutoff with its own clock, so keep client and server clocks synchronized. Subtract a margin from the current time when you want to keep recent records:
// Delete the records that were last updated more than one day agoclient.Truncate(null, "sandbox", "ufodata", DateTime.UtcNow.AddDays(-1));
// Close the connection to the serverclient.Close();Code block
Expand this section for a single code block to durably delete a record
using Aerospike.Client;
// Define host configurationHost config = new Host("127.0.0.1", 3000);// Establishes a connection to the serverAerospikeClient client = new AerospikeClient(null, config);
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001Key key = new Key("sandbox", "ufodata", 5001);
// Can use a previously defined write policy or create a new oneWritePolicy deletePolicy = new WritePolicy();deletePolicy.durableDelete = true;
// Durably delete a record with the write policyclient.Delete(deletePolicy, key);
// Close the connection to the serverclient.Close();