Unable to update document TTL

Hello,

I am attempting to insert documents to a couchbase bucket that has TTL set to default of 0 (disabled). I am inserting document and testing with .insert and .insert_multi and provided the optional ttl value. But when I check in the UI the documents do not have the TTL set.

My question: do I have to enable the Bucket TTL in order to set the TTL at a document level or is there I way for the document ttl to be updated? I also attempted .touch(document, ttl=ttl) and was not successful.

I am runnig using Python SDK 2.5.4 and Couchbase Enterprise 6.0.1

Hi @cjalvara,

First, the use of TTL/expiry can be confusing if it is an absolute unix timestamp it is pretty straightforward in N1QL and most of our SDKs (number of seconds since epoch) is just what you pass it. But you can also pass a fairly small number less than 30 days it is an offset for more details refer to document-expiration . You also need to understand the “precedence” as disclosed in buckets:expiration

So no you shouldn’t need to to set a bucket TTL if you do then you will be subject to the following constraints (provided no documents exist with a TTL already set):

  • Above that of Bucket TTL, the item’s TTL is reduced to the value of Bucket TTL.
  • Below that of Bucket TTL, the item’s TTL is left unchanged.
  • 0, the item’s TTL is reset to the value of Bucket TTL.

Second, although the TTL in always in seconds on the doc (property “expiration” in its metadata) there can be further confusion as in languages like Node (e.g. JavaScript) the native Date construct is in milliseconds and thus it depends on the Language, API and/or SDK used to determine what you need to pass (seconds, or milliseconds, or some date object).

The following will insert a document into bucket “source” and then set a TTL to expire in 10 minutes.

# This is a Python 2.5 SDK example
from couchbase.cluster import Cluster
from couchbase.cluster import PasswordAuthenticator
import time
cluster = Cluster('couchbase://localhost:8091')
authenticator = PasswordAuthenticator('Administrator', 'password')
cluster.authenticate(authenticator)
cb = cluster.open_bucket('source')
cb.upsert('SampleDocument2', {'a_key': 'a_value'})
cb.touch('SampleDocument2', ttl=10*60)

Hello @jon.strabala thanks for replying. That is what I am attempting to do. My goal is to set the expiration to 5 years but since I am not being successful with that I am trying to set ttl as 1000 with

cb.upsert(docid, document)
cb.touch(docid, ttl=1000)

With this I am able to see the newly inserted document in the couchbase but the expiration remains at 0. Any idea why this would be the case?

I just ran the code I gave on a 7.X beta couchbase-server with Python 2.7.16

>>> cb.upsert('SampleDocument2', {'a_key': 'a_value'})
OperationResult<rc=0x0, key=u'SampleDocument2', cas=0x1664978a28590000, tracing_context=0, tracing_output={'c': '0000000063852732/746cc529859d69aa', 'b': 'source', 'i': 8409897855176612071L, 'l': '127.0.0.1:42980', 's': 'kv:Unknown', 'r': 'localhost:11210', 't': 2500000L}>
>>> cb.touch('SampleDocument2', ttl=10*60)
OperationResult<rc=0x0, key=u'SampleDocument2', cas=0x1664978a28650000, tracing_context=0, tracing_output={'c': '0000000063852732/746cc529859d69aa', 'b': 'source', 'i': 16510411910136738598L, 'l': '127.0.0.1:42980', 's': 'kv:Unknown', 'r': 'localhost:11210', 't': 2500000L}>
>>>

As you can see I inserted my document (above) the response does not have the TTL, then I used the UI and looked at the document itself and specifically it’s meta data and I see the “expiration”: 1613581785 as expected seconds since epoch.

 {
      "meta": {
        "id": "SampleDocument2",
        "rev": "4-1664978a28650000602d4dd902000000",
        "expiration": 1613581785,
        "flags": 33554432,
        "type": "json"
      },
      "xattrs": {}
}

And after 10 minutes the document was gone when I accessed it the UI again. You might want to test with something smaller say 60 seconds:

cb.touch(‘SampleDocument2’, ttl=60)

Note I only have tested the script I gave you with version 6.5 and 7.X beta, I don’t run 6.0.1 as it’s quite old. But it should work as the docs seem to be unchanged WRT touch.

Did you adjust your bucket at all?

Try creating a new temp bucket just to test as you may have inadvertently set the TTL on the bucket.

You can also try using the cbc command line (non python - remove it from the equation) to see if you can set the TTL.

/opt/couchbase/bin/cbc \
    create SampleDocument2 -V '{"a_key": "a_value"}' -U couchbase://localhost/source \
    -u Administrator -P password \
    --expiry=60