--- layout: "docs" page_title: "Commands: KV Delete" sidebar_current: "docs-commands-kv-delete" --- # Consul KV Delete Command: `consul kv delete` The `kv delete` command removes the value from Consul's key-value store at the given path. If no key exists at the path, no action is taken. ## Usage Usage: `consul kv delete [options] KEY_OR_PREFIX` #### API Options <%= partial "docs/commands/http_api_options" %> #### KV Delete Options * `-cas` - Perform a Check-And-Set operation. If this value is specified without -modify-index, the key will first be fetched and the resulting ModifyIndex will be used on the next query. The default value is false. * `-modify-index=` - Unsigned integer representing the ModifyIndex of the key. This is often combined with the -cas flag, but it can be specified for any key. The default value is 0. * `-recurse` - Recursively delete all keys with the path. The default value is false. ## Examples To remove the value for the key named "redis/config/connections" in the key-value store: ``` $ 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: ``` $ 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: ``` $ 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 ``` It is also possible to have Consul fetch the current ModifyIndex before making the query, by omitting the `-modify-index` flag. If the data is changed between the initial read and the write, the operation will fail. ``` $ consul kv delete -cas redis/config/connections Success! Deleted key: redis/config/connections ``` To recursively delete all keys that start with a given prefix, specify the `-recurse` flag: ``` $ 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: ``` $ consul kv delete -cas -recurse redis/ Cannot specify both -cas and -recurse! ```