2016-09-26 15:12:40 +00:00
|
|
|
---
|
2020-09-01 15:14:13 +00:00
|
|
|
layout: commands
|
2020-04-07 18:55:19 +00:00
|
|
|
page_title: 'Commands: KV Delete'
|
2023-01-26 18:42:13 +00:00
|
|
|
description: >-
|
|
|
|
The `consul kv delete` command removes a value from Consul's key/value store.
|
2016-09-26 15:12:40 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
# Consul KV Delete
|
|
|
|
|
|
|
|
Command: `consul kv delete`
|
|
|
|
|
2023-01-25 16:52:43 +00:00
|
|
|
Corresponding HTTP API Endpoint: [\[DELETE\] /v1/kv/:key](/consul/api-docs/kv#delete-key)
|
2022-01-10 17:40:11 +00:00
|
|
|
|
2017-04-04 16:33:22 +00:00
|
|
|
The `kv delete` command removes the value from Consul's KV store at the
|
2016-09-26 15:12:40 +00:00
|
|
|
given path. If no key exists at the path, no action is taken.
|
|
|
|
|
2023-01-25 16:52:43 +00:00
|
|
|
The table below shows this command's [required ACLs](/consul/api-docs/api-structure#authentication). Configuration of
|
|
|
|
[blocking queries](/consul/api-docs/features/blocking) and [agent caching](/consul/api-docs/features/caching)
|
2022-01-10 21:44:56 +00:00
|
|
|
are not supported from commands, but may be from the corresponding HTTP endpoint.
|
|
|
|
|
|
|
|
| ACL Required |
|
|
|
|
| ------------ |
|
|
|
|
| `key:write` |
|
|
|
|
|
2016-09-26 15:12:40 +00:00
|
|
|
## Usage
|
|
|
|
|
|
|
|
Usage: `consul kv delete [options] KEY_OR_PREFIX`
|
|
|
|
|
2022-07-27 06:17:11 +00:00
|
|
|
#### Command Options
|
2016-09-26 15:12:40 +00:00
|
|
|
|
2022-07-27 06:17:11 +00:00
|
|
|
- `-cas` - Perform a Check-And-Set operation.
|
|
|
|
To use this option, the `-modify-index` flag must also be set.
|
|
|
|
The default value is `false`.
|
2020-04-07 23:56:08 +00:00
|
|
|
|
2022-07-27 06:17:11 +00:00
|
|
|
- `-modify-index=<int>` - Specifies an unsigned integer that represents
|
|
|
|
the `ModifyIndex` of the key. Used in combination with the `-cas` flag.
|
2016-09-26 15:12:40 +00:00
|
|
|
|
2022-07-27 06:17:11 +00:00
|
|
|
- `-recurse` - Recursively delete all keys with the path. The default value is
|
|
|
|
`false`.
|
2019-12-10 02:26:41 +00:00
|
|
|
|
2022-07-27 06:17:11 +00:00
|
|
|
#### Enterprise Options
|
2019-12-10 02:26:41 +00:00
|
|
|
|
2024-07-18 17:02:45 +00:00
|
|
|
@include 'cli-http-api-partition-options.mdx'
|
2021-12-15 01:55:21 +00:00
|
|
|
|
2022-07-27 06:17:11 +00:00
|
|
|
@include 'http_api_namespace_options.mdx'
|
2016-09-26 15:12:40 +00:00
|
|
|
|
2022-07-27 06:17:11 +00:00
|
|
|
#### API Options
|
2016-09-26 15:12:40 +00:00
|
|
|
|
2022-07-27 06:17:11 +00:00
|
|
|
@include 'http_api_options_client.mdx'
|
2016-09-26 15:12:40 +00:00
|
|
|
|
2022-07-27 06:17:11 +00:00
|
|
|
@include 'http_api_options_server.mdx'
|
2016-09-26 15:12:40 +00:00
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
To remove the value for the key named "redis/config/connections" in the
|
2017-04-04 16:33:22 +00:00
|
|
|
KV store:
|
2016-09-26 15:12:40 +00:00
|
|
|
|
2020-05-19 18:32:38 +00:00
|
|
|
```shell-session
|
2016-09-26 15:12:40 +00:00
|
|
|
$ consul kv delete redis/config/connections
|
|
|
|
Success! Deleted key: redis/config/connections
|
|
|
|
```
|
|
|
|
|
|
|
|
If the key does not exist, the command will not error, and a success message
|
|
|
|
will be returned:
|
|
|
|
|
2020-05-19 18:32:38 +00:00
|
|
|
```shell-session
|
2016-09-26 15:12:40 +00:00
|
|
|
$ consul kv delete not-a-real-key
|
|
|
|
Success! Deleted key: not-a-real-key
|
|
|
|
```
|
|
|
|
|
|
|
|
To only delete a key if it has not been modified since a given index, specify
|
|
|
|
the `-cas` and `-modify-index` flags:
|
|
|
|
|
2020-05-19 18:32:38 +00:00
|
|
|
```shell-session
|
2016-09-26 15:12:40 +00:00
|
|
|
$ consul kv get -detailed redis/config/connections | grep ModifyIndex
|
|
|
|
ModifyIndex 456
|
|
|
|
|
|
|
|
$ consul kv delete -cas -modify-index=123 redis/config/connections
|
|
|
|
Error! Did not delete key redis/config/connections: CAS failed
|
|
|
|
|
|
|
|
$ consul kv delete -cas -modify-index=456 redis/config/connections
|
|
|
|
Success! Deleted key: redis/config/connections
|
|
|
|
```
|
|
|
|
|
|
|
|
To recursively delete all keys that start with a given prefix, specify the
|
|
|
|
`-recurse` flag:
|
|
|
|
|
2020-05-19 18:32:38 +00:00
|
|
|
```shell-session
|
2016-09-26 15:12:40 +00:00
|
|
|
$ consul kv delete -recurse redis/
|
|
|
|
Success! Deleted keys with prefix: redis/
|
|
|
|
```
|
|
|
|
|
|
|
|
!> **Trailing slashes are important** in the recursive delete operation, since
|
|
|
|
Consul performs a greedy match on the provided prefix. If you were to use "foo"
|
|
|
|
as the key, this would recursively delete any key starting with those letters
|
|
|
|
such as "foo", "food", and "football" not just "foo". To ensure you are deleting
|
|
|
|
a folder, always use a trailing slash.
|
|
|
|
|
|
|
|
It is not valid to combine the `-cas` option with `-recurse`, since you are
|
|
|
|
deleting multiple keys under a prefix in a single operation:
|
|
|
|
|
2020-05-19 18:32:38 +00:00
|
|
|
```shell-session
|
2016-09-26 15:12:40 +00:00
|
|
|
$ consul kv delete -cas -recurse redis/
|
|
|
|
Cannot specify both -cas and -recurse!
|
|
|
|
```
|