Delete based on secondary index

deletion

#1
NameSpace - person
Set - personData
PersonId (Key) | name | birthDate 

NameSpace - person
Set - cityVisitedData
PersonId+CityVisited (Key)| cityName | personId

Here person can have many city visited. Use case is that if someone deletes person record then all city visited record should be deleted. I have secondary index on personid for cityVisitedData set. If i delete using PK (PersonId+CityVisited) then only one record will be deleted. But I want to delete all records containing person id. I checked the delete api where it takes key and also tried passing personId in that key but it does not delete anything.

Key key = new Key("person", "cityVisitedData", Value.get(personId));
client.delete(writePolicy, key);

The above code does not delete any record.


#2

That’s because a secondary index is used for querying (reading). If you want to delete the records matching a specific query you need to apply a record UDF containing an aerospike:remove() to the query.

There’s an example for how to do this in the Java client section of our site: Transforming Records.


#3

Thanks Roben,

I was able to delete the record using udf by providing lua script which takes record as an input. I passed filter statement to execute lua script.


#4

Great. It’s best to do that kind of operation as a background scan.