Skip to content
Latchkey

consul kv delete: Remove KV Keys in CI

consul kv delete removes a single key, or an entire prefix with -recurse, from the Consul key/value store.

Cleanup steps and teardown jobs use kv delete to drop stale config. -recurse wipes a whole tree, so scope the prefix carefully.

What it does

consul kv delete removes the key at the given path. With -recurse it deletes every key under the prefix in one call. -cas with -modify-index makes the delete conditional on the key being unchanged.

Common usage

Terminal
consul kv delete config/app/log_level
# delete an entire subtree
consul kv delete -recurse config/app/
# conditional delete
consul kv delete -cas -modify-index=42 config/app/key

Options

FlagWhat it does
-recurseDelete all keys under the prefix
-casOnly delete if -modify-index matches
-modify-index <n>Expected ModifyIndex for a -cas delete

In CI

Deleting a non-existent key still exits 0, so teardown steps are idempotent. Treat -recurse like rm -r: a too-broad prefix (config/ instead of config/app/) wipes far more than intended, and there is no undo.

Common errors in CI

"Error! Did not delete key <key>: CAS failed" means the key changed since your -modify-index. "Unexpected response code: 403 (Permission denied)" means the token lacks key:write on the prefix. A plain delete of a prefix path without -recurse only removes the exact key, leaving children behind.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →