---
type: api
title: Create or update a policy
description: Create or update a snapshot lifecycle policy.
url: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch/operation/operation-slm-put-lifecycle
resource: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch/operation/operation-slm-put-lifecycle
products:
  - Elasticsearch
---

# Create or update a policy

Availability: 7.4+

## Paths

- `PUT` `/_slm/policy/{policy_id}`

- [Api key auth](/elastic/docs-builder/docs/4371/api/doc/elasticsearch/authentication#apikeyauth)
- [Basic auth](/elastic/docs-builder/docs/4371/api/doc/elasticsearch/authentication#basicauth)
- [Bearer auth](/elastic/docs-builder/docs/4371/api/doc/elasticsearch/authentication#bearerauth)

#### Path Parameters

- `policy_id` (string Name)
  The identifier for the snapshot lifecycle policy you want to create or update.

## Description

Create or update a snapshot lifecycle policy.
If the policy already exists, this request increments the policy version.
Only the latest version of a policy is stored.

## Required authorization

* Index privileges: `manage`
* Cluster privileges: `manage_slm`

## Query String Parameters

- `master_timeout` (string Duration · default: 30s)
  The period to wait for a connection to the master node.
If no response is received before the timeout expires, the request fails and returns an error.
To indicate that the request should never timeout, set it to `-1`.
- `timeout` (string Duration · default: 30s)
  The period to wait for a response.
If no response is received before the timeout expires, the request fails and returns an error.
To indicate that the request should never timeout, set it to `-1`.

## Request Body

`application/json`

- `config` ({} Configuration) — optional
  Configuration for each snapshot created by the policy.
  - `ignore_unavailable` (boolean · default: false) — optional
    If false, the snapshot fails if any data stream or index in indices is missing or closed. If true, the snapshot ignores missing or closed data streams and indices.
  - `indices` (string Indices) — optional
    A comma-separated list of data streams and indices to include in the snapshot. Multi-index syntax is supported.
By default, a snapshot includes all data streams and indices in the cluster. If this argument is provided, the snapshot only includes the specified data streams and clusters.
  - `include_global_state` (boolean · default: true) — optional
    If true, the current global state is included in the snapshot.
  - `feature_states` ([] string) — optional
    A list of feature states to be included in this snapshot. A list of features available for inclusion in the snapshot and their descriptions be can be retrieved using the get features API.
Each feature state includes one or more system indices containing data necessary for the function of that feature. Providing an empty array will include no feature states in the snapshot, regardless of the value of include_global_state. By default, all available feature states will be included in the snapshot if include_global_state is true, or no feature states if include_global_state is false.
  - `metadata` (Metadata) — optional
    Attaches arbitrary metadata to the snapshot, such as a record of who took the snapshot, why it was taken, or any other useful data. Metadata must be less than 1024 bytes.
  - `partial` (boolean · default: false) — optional
    If false, the entire snapshot will fail if one or more indices included in the snapshot do not have all primary shards available.
- `name` (string Name) — optional
  Name automatically assigned to each snapshot created by the policy. Date math is supported. To prevent conflicting snapshot names, a UUID is automatically appended to each snapshot name.
- `repository` (string) — optional
  Repository used to store snapshots created by this policy. This repository must exist prior to the policy’s creation. You can create a repository using the snapshot repository API.
- `retention` ({} Retention) — optional
  Retention rules used to retain and delete snapshots created by the policy.
  - `expire_after` (string Duration) — required
    Time period after which a snapshot is considered expired and eligible for deletion. SLM deletes expired snapshots based on the slm.retention_schedule.
  - `max_count` (number) — required
    Maximum number of snapshots to retain, even if the snapshots have not yet expired. If the number of snapshots in the repository exceeds this limit, the policy retains the most recent snapshots and deletes older snapshots.
  - `min_count` (number) — required
    Minimum number of snapshots to retain, even if the snapshots have expired.
- `schedule` (string) — optional
  Periodic or absolute schedule at which the policy creates snapshots. SLM applies schedule changes immediately.

## Response

`application/json`

- `acknowledged` (boolean)
  For a successful response, this value is always true. On failure, an exception is returned instead.
## Code Examples

#### Console

```console
PUT /_slm/policy/daily-snapshots
{
  "schedule": "0 30 1 * * ?",
  "name": "<daily-snap-{now/d}>",
  "repository": "my_repository",
  "config": {
    "indices": ["data-*", "important"],
    "ignore_unavailable": false,
    "include_global_state": false
  },
  "retention": {
    "expire_after": "30d",
    "min_count": 5,
    "max_count": 50
  }
}
```

#### JavaScript

```javascript
const response = await client.slm.putLifecycle({
  policy_id: "daily-snapshots",
  schedule: "0 30 1 * * ?",
  name: "<daily-snap-{now/d}>",
  repository: "my_repository",
  config: {
    indices: ["data-*", "important"],
    ignore_unavailable: false,
    include_global_state: false,
  },
  retention: {
    expire_after: "30d",
    min_count: 5,
    max_count: 50,
  },
});
```

#### Python

```python
resp = client.slm.put_lifecycle(
    policy_id="daily-snapshots",
    schedule="0 30 1 * * ?",
    name="<daily-snap-{now/d}>",
    repository="my_repository",
    config={
        "indices": [
            "data-*",
            "important"
        ],
        "ignore_unavailable": False,
        "include_global_state": False
    },
    retention={
        "expire_after": "30d",
        "min_count": 5,
        "max_count": 50
    },
)
```

#### curl

```curl
curl -X PUT "$ELASTICSEARCH_URL/_slm/policy/daily-snapshots" \
  -H "Authorization: ApiKey $ELASTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "schedule": "0 30 1 * * ?",
    "name": "\u003Cdaily-snap-{now/d}\u003E",
    "repository": "my_repository",
    "config": {
      "indices": [
        "data-*",
        "important"
      ],
      "ignore_unavailable": false,
      "include_global_state": false
    },
    "retention": {
      "expire_after": "30d",
      "min_count": 5,
      "max_count": 50
    }
  }'
```

#### Java

```java
client.slm().putLifecycle(p -> p
    .config(c -> c
        .ignoreUnavailable(false)
        .indices(List.of("data-*","important"))
        .includeGlobalState(false)
    )
    .name("<daily-snap-{now/d}>")
    .policyId("daily-snapshots")
    .repository("my_repository")
    .retention(r -> r
        .expireAfter(e -> e
            .time("30d")
        )
        .maxCount(50)
        .minCount(5)
    )
    .schedule("0 30 1 * * ?")
);
```

#### C#

```c#
var response = await client.SnapshotLifecycleManagement
    .PutLifecycleAsync(policyId: "daily-snapshots", d1 => d1
        .Config(d2 => d2
            .IgnoreUnavailable(false)
            .IncludeGlobalState(false)
            .Indices(new[] { "data-*", "important" })
        )
        .Name("<daily-snap-{now/d}>")
        .Repository("my_repository")
        .Retention(d2 => d2
            .ExpireAfter("30d")
            .MaxCount(50)
            .MinCount(5)
        )
        .Schedule("0 30 1 * * ?")
    );
```

#### PHP

```php
$resp = $client->slm()->putLifecycle([
    "policy_id" => "daily-snapshots",
    "body" => [
        "schedule" => "0 30 1 * * ?",
        "name" => "<daily-snap-{now/d}>",
        "repository" => "my_repository",
        "config" => [
            "indices" => array(
                "data-*",
                "important",
            ),
            "ignore_unavailable" => false,
            "include_global_state" => false,
        ],
        "retention" => [
            "expire_after" => "30d",
            "min_count" => 5,
            "max_count" => 50,
        ],
    ],
]);
```

#### Ruby

```ruby
response = client.slm.put_lifecycle(
  policy_id: "daily-snapshots",
  body: {
    "schedule": "0 30 1 * * ?",
    "name": "<daily-snap-{now/d}>",
    "repository": "my_repository",
    "config": {
      "indices": [
        "data-*",
        "important"
      ],
      "ignore_unavailable": false,
      "include_global_state": false
    },
    "retention": {
      "expire_after": "30d",
      "min_count": 5,
      "max_count": 50
    }
  }
)
```

