How to set keys ttl while executing batchWrite?


Is there any possibility to set per key TTL in batchWrite? I came to the following code, but it doesn’t work for not existent keys.

        const policy = new Aerospike.BatchWritePolicy({
            exists: Aerospike.policy.exists.CREATE_OR_REPLACE,
            key: Aerospike.policy.key.SEND,
        const batchRecords ={key, value, ttl}) => ({
            type: batchType.BATCH_WRITE,
            key: this.makeKey(key),
            ops: [
                operations.write(DATA_BIN, value),
                operations.touch(ttl), // it doesn't work for unexistent keys
        await client.batchWrite(batchRecords, policy)

I’m using nodejs aerospike client version 5.10.0

Thank you very much!

Hi @Konstantin_Andreev,

Currently, the only way to set TTL for a non-existent record is using client.put with metadata including a TTL, then operating after.

In the next release of the Aerospike Client, we will add a TTL field to be used in batchWrite policies. The release should be coming in the next few weeks, so I will post here when the release is out with example code on how to use it.

1 Like