Skip to content

Delete records

Learn how to delete records from Aerospike using the Developer SDK. This guide covers simple deletion, conditional deletes, and handling deletion of non-existent records.

Delete a record

Use delete() to remove a record by its key:

DataSet users = DataSet.of("test", "users");
session.delete(users.id("user-1"))
.execute();

Check if delete succeeded

Use the delete result row to determine whether a record was actually deleted:

import com.aerospike.client.sdk.RecordResult;
import com.aerospike.client.sdk.RecordStream;
RecordStream deleteStream = session.delete(users.id("user-1")).execute();
boolean deleted = deleteStream.getFirst().map(RecordResult::asBoolean).orElse(false);
if (deleted) {
System.out.println("Record deleted successfully");
} else {
System.out.println("Record didn't exist");
}

Conditional delete (generation check)

Delete only if the record hasn’t been modified since you last read it:

import com.aerospike.client.sdk.AerospikeException;
import com.aerospike.client.sdk.Record;
import com.aerospike.client.sdk.RecordResult;
import com.aerospike.client.sdk.RecordStream;
// Read the record first
RecordStream readStream = session.query(users.id("user-1")).execute();
Record user = readStream.getFirstRecord();
// Delete only if generation matches
try {
session.delete(users.id("user-1"))
.ensureGenerationIs(user.generation)
.execute();
System.out.println("Deleted successfully");
} catch (GenerationException e) {
System.out.println("Record was modified, delete aborted");
}

Delete only if exists

By default, deleting a non-existent record is a no-op. In Java, use exists() first if you need a hard failure when the record is missing. In Python, check existence first and then delete:

// There is no deleteOnly() in the Java SDK; check existence first if you must fail when absent.
boolean existed = session.exists(users.id("user-1")).execute().getFirstBoolean().orElse(false);
if (!existed) {
System.out.println("Record not found");
} else {
session.delete(users.id("user-1")).execute().close();
System.out.println("Record deleted");
}

Delete with durability options

For critical deletes, ensure durability. These can either be done on a per-call basis, or set on the session level via a Behavior.

// Use a durable session for important deletes
session.delete(users.id("important-record"))
.withDurableDelete()
.execute();

📖 Learn more: Behaviors explains durability options.

Batch delete

For deleting multiple records efficiently, see Batch Operations.

// Quick preview - see Batch Operations for full details
RecordStream deleteStream = session.delete(users.ids("user-1", "user-2", "user-3")).execute();

Soft delete pattern

Instead of permanently deleting, mark records as deleted:

import com.aerospike.client.sdk.Record;
import com.aerospike.client.sdk.RecordResult;
import com.aerospike.client.sdk.RecordStream;
import java.util.ArrayList;
import java.util.List;
// Soft delete: mark as deleted instead of removing
session.update(users.id("user-1"))
.bin("deleted").setTo(true)
.bin("deleted_at").setTo(System.currentTimeMillis())
.execute();
// When querying, filter out deleted records
RecordStream stream = session.query(users)
.where("$.deleted == false")
.execute();
List<Record> activeUsers = stream.stream().asList();
}
stream.close();

Delete with TTL (auto-expire)

Instead of explicit deletion, let records expire automatically. Note: do not decrease the expiration time on a record.

import java.time.Duration;
// Set TTL to expire in 1 minute (deferred delete)
session.touch(users.id("user-1"))
.expireRecordAfter(Duration.ofMinutes(1))
.execute();

Complete example

import com.aerospike.client.sdk.Cluster;
import com.aerospike.client.sdk.ClusterDefinition;
import com.aerospike.client.sdk.DataSet;
import com.aerospike.client.sdk.Record;
import com.aerospike.client.sdk.RecordResult;
import com.aerospike.client.sdk.RecordStream;
import com.aerospike.client.sdk.Session;
import com.aerospike.client.sdk.policy.Behavior;
public class DeleteRecordsExample {
public static void main(String[] args) {
try (Cluster cluster = new ClusterDefinition("localhost", 3000).connect()) {
Session session = cluster.createSession(Behavior.DEFAULT);
DataSet users = DataSet.of("test", "users");
String deleteKey = "delete-example-user";
String conditionalKey = "delete-example-conditional";
// Cleanup so the example is repeatable.
session.delete(users.id(deleteKey)).execute().close();
session.delete(users.id(conditionalKey)).execute().close();
// Create a test record
session.insert(users)
.bins("name")
.id(deleteKey).values("Test User")
.execute();
// Simple delete
RecordStream deletedStream = session.delete(users.id(deleteKey)).execute();
boolean deleted = deletedStream.getFirst().map(RecordResult::asBoolean).orElse(false);
System.out.println("Deleted: " + deleted);
// Try to delete again (returns false)
RecordStream secondDeletedStream = session.delete(users.id(deleteKey)).execute();
deleted = secondDeletedStream.getFirst().map(RecordResult::asBoolean).orElse(false);
secondDeletedStream.close();
System.out.println("Second delete: " + deleted);
// Conditional delete example
session.insert(users)
.bins("name")
.id(conditionalKey).values("Conditional User")
.execute();
RecordStream rs = session.query(users.id(conditionalKey)).execute();
Record record = rs.getFirstRecord();
session.delete(users.id(conditionalKey))
.ensureGenerationIs(record.generation)
.execute();
System.out.println("Conditional delete succeeded");
}
}
}

API reference summary

MethodDescriptionLink
delete()Delete a record by keyJava · Python
.ensureGenerationIs(gen) / .ensure_generation_is(gen)Conditional delete
(No deleteOnly / delete_only in either SDK)Use exists() then delete(), or inspect the delete result

Next steps

Feedback

Was this page helpful?

What type of feedback are you giving?

What would you like us to know?

+Capture screenshot

Can we reach out to you?