---
layout: docs
page_title: Set max data versions
description: >-
   Use max-versions to automatically destroy older data versions in the kv v2
   plugin.
---

# Set max data versions in key/value v2

Limit the number of active versions for a `kv` v2 secret path so Vault
permanently deletes (destroys) older data versions automatically.

<Tip title="Assumptions">

- You have [set up a `kv` v2 plugin](/vault/docs/secrets/kv/kv-v2/setup). 
- Your authentication token has `create` and `update` permissions for the `kv`
  v2 plugin.

</Tip>

<Tabs>

<Tab heading="CLI" group="cli">

Use [`vault kv metadata put`](/vault/docs/command/kv/metadata) to change the max
number of versions allowed for a `kv` mount path: 

```shell-session
$ vault kv metadata put          \
   -max-versions <max_versions>  \
   -mount <mount_path>           \
   <secret_path>
```

For example: 

<CodeBlockConfig hideClipboard="true">

```shell-session
$ vault kv metadata put \
   -max-versions 5      \
   -mount shared        \
   dev/square-api

Success! Data written to: shared/metadata/dev/square-api
```
</CodeBlockConfig>


Vault now auto-deletes data when the number of versions exceeds 5:

<CodeBlockConfig hideClipboard="true" highlight="14">

```shell-session
$ vault kv metadata get -mount shared dev/square-api

======== Metadata Path ========
shared/metadata/dev/square-api

========== Metadata ==========
Key                     Value
---                     -----
cas_required            false
created_time            2024-11-13T21:51:50.898782695Z
current_version         4
custom_metadata         <nil>
delete_version_after    0s
max_versions            5
oldest_version          0
updated_time            2024-11-14T22:32:42.29534643Z

====== Version 1 ======
Key              Value
---              -----
created_time     2024-11-13T21:51:50.898782695Z
deletion_time    n/a
destroyed        false
```

</CodeBlockConfig>

</Tab>

<Tab heading="GUI" group="gui">

@include 'gui-instructions/plugins/kv/open-overview.mdx'

- Select the **Metadata** tab.
- Click **Edit metadata >**.
- Update the **Maximum number of versions** field.
- Click **Update**.

![Partial screenshot of the Vault GUI showing the "Edit Secret Metadata" screen](/img/gui/kv/edit-metadata.png)

</Tab>

<Tab heading="API" group="api">

1. Create a JSON file with the metadata field `max_versions` set to the maximum
   number of versions you want to allow.

1. Make a `POST` call to
   [`/{plugin_mount_path}/metadata/{secret_path}`](/vault/api-docs/secret/kv/kv-v2#create-update-metadata)
   with the JSON data file:
    ```shell-session
    $ curl                                      \
      --request POST                            \
      --header "X-Vault-Token: ${VAULT_TOKEN}"  \
      --data @metadata.json                     \
      ${VAULT_ADDR}/v1/<plugin_mount_path>/metadata/<secret_path>
    ```

For example:

<CodeBlockConfig hideClipboard="true">

```json
{
  "max_versions": 10
}
```

</CodeBlockConfig>

<CodeBlockConfig hideClipboard="true">

```shell-session
$ curl                                       \
    --request POST                           \
    --header "X-Vault-Token: ${VAULT_TOKEN}" \
    --data @metadata.json                    \
    ${VAULT_ADDR}/v1/shared/metadata/dev/square-api
```

`/{plugin_mount_path}/metadata/{secret_path}` does not return data on success.

</CodeBlockConfig>

</Tab>

</Tabs>