Delete
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.
import ( "fmt" "github.com/aerospike/aerospike-client-go/v6")
// Establishes a connection to the serverclient, err := aerospike.NewClient("127.0.0.1", 3000)if err != nil { log.Fatal(err)}defer client.Close()
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001key, err := aerospike.NewKey("sandbox", "ufodata", 5001)if err != nil { log.Fatal(err)}
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 onedeletePolicy := aerospike.NewWritePolicy(0,0)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 policy_, err = client.Delete(nil, key)if err != nil { log.Fatal(err)}
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_, err = client.Delete(deletePolicy, key)if err != nil { log.Fatal(err)}
Code block
Expand this section for a single code block to durably delete a record
import ( "fmt" "github.com/aerospike/aerospike-client-go/v6")
func main() { // Establishes a connection to the server client, err := aerospike.NewClient("127.0.0.1", 3000) if err != nil { log.Fatal(err) } defer client.Close()
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001 key, err := aerospike.NewKey("sandbox", "ufodata", 5001) if err != nil { log.Fatal(err) }
// Can use a previously defined write policy or create a new one deletePolicy := aerospike.NewWritePolicy(0,0) deletePolicy.DurableDelete = true
// Durably delete a record with the write policy _, err = client.Delete(deletePolicy, key) if err != nil { log.Fatal(err) }}