Schedule icon
Request icon
If icon
Fail icon
OutputValues icon
Loop icon
Log icon
SlackIncomingWebhook icon

Archive Elasticsearch indices to a snapshot before retention deletes them

Elasticsearch retention that never loses data. Kestra snapshots each expired index, checks every shard, then deletes it by exact name. Write index protected.

Categories
DataInfrastructure

Retention policies delete old indices. When someone later needs last quarter's logs for an audit or an incident review, the data is gone. This flow archives each expired index to a snapshot repository first, checks that the snapshot is SUCCESS with every shard copied and only then deletes that one index, by its exact name.

Safeguards:

  • A red cluster or a repository that fails _verify stops the run before anything is touched.
  • The index behind the write alias is never selected, whatever its age. An alias that does not resolve stops the run.
  • max_indices_per_run caps the damage of a wrong pattern or retention value.
  • Deletes use the exact index name, never a wildcard, so the cluster default action.destructive_requires_name: true stays on.
  • io.kestra.plugin.elasticsearch.Request reports success on any HTTP status, so the flow checks every status code and the snapshot body itself.

This blueprint was created by zkasuran.

How it works

  1. cluster_health and verify_repository (io.kestra.plugin.elasticsearch.Request) read _cluster/health and call POST _snapshot/<repository>/_verify.
  2. preflight (io.kestra.plugin.core.flow.If) continues only on a non-red cluster and a verified repository. Otherwise preflight_failed ends the run as FAILED with both responses.
  3. list_indices reads index.creation_date for every index matching index_pattern. write_index resolves the write alias.
  4. plan (io.kestra.plugin.core.output.OutputValues) selects indices created before now minus retention_days, oldest first. selected removes the write index and applies max_indices_per_run.
  5. archive_and_delete (io.kestra.plugin.core.flow.Loop, one at a time) handles each index:
    • snapshot: PUT _snapshot/<repository>/retention-<index>-<timestamp>?wait_for_completion=true for that index only.
    • snapshot_ok (If): state SUCCESS, shards.failed == 0 and successful == total. Then delete_index sends DELETE <index> and checks for HTTP 200 and acknowledged: true.
    • Any failure ends the run through Fail. The index is kept.
  6. log_done lists what was archived and deleted. errors posts a Slack alert when notify_slack is on.

Inputs

  • index_pattern (STRING, default app-logs-*): indices covered by the policy.
  • write_alias (STRING, default app-logs-write): its write index is always kept.
  • retention_days (FLOAT, default 30): age in days, based on index.creation_date.
  • snapshot_repository (STRING, default retention_repo): a registered snapshot repository.
  • max_indices_per_run (INT, default 10): safety cap.
  • notify_slack (BOOL, default false): post results to Slack.

Prerequisites

  • Elasticsearch 8.x with a snapshot repository registered, for example PUT _snapshot/retention_repo {"type": "s3", "settings": {"bucket": "es-archive"}}.
  • A user with monitor, manage_snapshots, view_index_metadata and delete_index on the pattern.

Secrets

  • ELASTICSEARCH_HOST: cluster URL, for example https://es.example.com:9200.
  • ELASTICSEARCH_USERNAME and ELASTICSEARCH_PASSWORD: the user above.
  • SLACK_WEBHOOK_URL: Slack incoming webhook. Only needed when notify_slack is true.

Quick start

  1. Register the snapshot repository and add the secrets.
  2. Run the flow with the defaults. With no index older than 30 days the log says so and nothing changes.
  3. On a test cluster, run it with a small retention_days such as 0.001 and max_indices_per_run: 1. One index is snapshotted, then deleted.
  4. Restore an archived index when needed: POST _snapshot/<repository>/<snapshot>/_restore {"indices": "<index>"}.

Expected outputs

  • One snapshot per deleted index, named retention-<index>-<yyyyMMdd-HHmmss>, with metadata.kestra_execution set to the execution id.
  • outputs.selected.values.indices: the indices archived and deleted in this run.

How to extend

  • Read the age from a date in the index name when indices are reindexed and lose their original creation date.
  • Add a second policy that deletes snapshots older than the legal hold period.
  • Run one flow per index family with different retention_days.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.