{
  "id": "redis-expiration-example",
  "title": "Set custom expiration times / TTL",
  "url": "https://redis.io/docs/latest/integrate/redis-data-integration/data-pipelines/transform-examples/redis-expiration-example/",
  "summary": "",
  "tags": [
    "docs",
    "integrate",
    "rs",
    "rdi"
  ],
  "last_updated": "2026-04-01T08:10:08-05:00",
  "page_type": "content",
  "content_hash": "e2621ff0c655710d6c050c6515ff41689e14613ab27217b00bc006b92e85b72a",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "You can configure custom key expiration times (TTL) for keys written to Redis by using the `expire` parameter in the `output` section of the job file. This parameter specifies the duration, in seconds, that a newly created key will remain in Redis before being automatically deleted. If the `expire` parameter is not provided, the keys will persist indefinitely.\n\nThere are two ways to set the expiration time:\n\n- as a static value\n- as a dynamic value using a JMESPath or SQL expression"
    },
    {
      "id": "static-expiration-time",
      "title": "Static expiration time",
      "role": "content",
      "text": "The following example sets the expiration time to 100 seconds for all keys:\n\n[code example]"
    },
    {
      "id": "dynamic-expiration-time",
      "title": "Dynamic expiration time",
      "role": "content",
      "text": "You can use a JMESPath or SQL expression to set the expiration time dynamically when it is based on a field in the source data. For example, you can set the expiration time to the value of a `ttl` field in the source data:\n\n[code example]"
    },
    {
      "id": "dynamic-expiration-time-based-on-a-date-datetime-or-timestamp-field",
      "title": "Dynamic expiration time based on a date, datetime, or timestamp field",
      "role": "content",
      "text": "In some cases, you can also set the expiration time based on a field that contains a date, datetime, or timestamp value, but it depends on the source database and the data types it supports. See the examples below for your specific source database and data type.\n\nThere are two main approaches you can use to set the expiration time based on a date, datetime, or timestamp field:\n\n- For values representing an elapsed time since epoch start (in milliseconds, for example), you have to convert the value to seconds since epoch and then subtract the current time (also in seconds since epoch). The difference between the two is the time until expiration.\n\n    [code example]\n\n- For values matching the subset of ISO 8601 supported by SQLite (for example, `2023-10-01T12:00:00`, `2023-10-01T12:00:00Z`, or `2025-06-05T13:40:14.784000+02:00`), you can use the `STRFTIME` function to convert the value to seconds since epoch and subtract the current time in seconds since epoch from it.\n\n  [code example]\n\nFor more examples of how to manipulate date and time values, see [Formatting date and time values]()."
    }
  ],
  "examples": [
    {
      "id": "static-expiration-time-ex0",
      "language": "yaml",
      "code": "name: Static expiration example\noutput:\n  - uses: redis.write\n    with:\n      data_type: hash\n      expire: 100",
      "section_id": "static-expiration-time"
    },
    {
      "id": "dynamic-expiration-time-ex0",
      "language": "yaml",
      "code": "name: Dynamic expiration from field\noutput:\n  - uses: redis.write\n    with:\n      data_type: hash\n      expire:\n        expression: ttl\n        language: jmespath",
      "section_id": "dynamic-expiration-time"
    },
    {
      "id": "dynamic-expiration-time-based-on-a-date-datetime-or-timestamp-field-ex0",
      "language": "yaml",
      "code": "name: Expiration from timestamp in milliseconds\n    output:\n        - uses: redis.write\n          with:\n            data_type: hash\n            expire:\n              # To set the expiration time to a date field, convert the value to\n              # seconds (e.g. divide it by 1000 if the fields has milliseconds precision)\n              # and subtract the current time in seconds since epoch.\n              expression: EXPIRES_TIMESTAMP / 1000 - STRFTIME('%s', 'now')\n              language: sql",
      "section_id": "dynamic-expiration-time-based-on-a-date-datetime-or-timestamp-field"
    },
    {
      "id": "dynamic-expiration-time-based-on-a-date-datetime-or-timestamp-field-ex1",
      "language": "yaml",
      "code": "name: Expiration from ISO 8601 datetime\n  output:\n    - uses: redis.write\n      with:\n        data_type: hash\n        expire:\n          language: sql\n          expression: STRFTIME('%s', EXPIRATION_TS) - STRFTIME('%s', 'now')",
      "section_id": "dynamic-expiration-time-based-on-a-date-datetime-or-timestamp-field"
    }
  ]
}
