Skip to content

Set Key Expiration - TTL


This operation sets a time-to-live(TTL) timeout on a specific key. When the timeout expires, they key and the corresponding value are deleted. Note that, all keys are created without timeouts by default. This operation enables the timeout functionality.


[[[Backendless.shared hive:(NSString *)hiveName] storeType:(NSString *)keyName] expireAfterTtl:(NSInteger)ttl responseHandler:^(void)responseHandler errorHandler:^(Fault *)errorHandler];
Backendless.shared.hive(hiveName: String).storeType(keyName: String).expireAfter(ttl: Int, responseHandler: (() -> Void)!, errorHandler: ((Fault) -> Void)!)


Argument                Description
hiveName Name of a hive where the operation is performed. String value.
storeType This placeholder must be substituted with one of the following stores types: keyValueStore, listStore, mapStore, setStore, sortedSetStore.
keyName Key name to set the expiration timeout in seconds. String value.
seconds A timeout value in seconds. When the specified number of seconds elapses after the operation is complete, the key-value pair expires and is subsequently removed from the store. Integer value.

Return Value


If the specified key does not exist, returns the following Backendless error:

    "code": 27003,  
    "message": "no such key",  
    "errorData": {}  


The example below sets the 60 seconds timeout for the "fruits" key, once the timeout expires, the key gets deleted from the list store.

[[[Backendless.shared hive:@"groceryStore"] listStore:@"fruits"] expireAfterTtl:60 responseHandler:^{
    // handle response
} errorHandler:^(Fault *fault) {
    // handle error
Backendless.shared.hive("groceryStore").listStore("fruits").expireAfter(ttl: 60, responseHandler: {
    // handle response
}, errorHandler: { fault in
    // handle error


Argument                Description
"groceryStore" Name of a hive where the operation is performed.
"fruits" Key name to set the 60 seconds timeout for.

Codeless Reference


Argument                Description
hive name Name of a hive where the operation is performed.
type Storage type, can be one of the following: Key / Value, Sorted Set, Set, Map, List.
key name Key name to set a timeout for.
expire Expiration timeout type. Can be set to seconds after or on timestamp/date.

The after seconds identifies the number of seconds until the key expires.

The timestamp identifies a Unix timestamp/date when the key must expire. This option expects the number of seconds in the Unix format representing the exact date and time set for the key expiration.

Consider the following Key Value storage:


The example below sets an expiration timeout on the "Japan" key for 100 seconds, once the timeout expires, the "Japan" key gets deleted from the store.