y
y
yq
Search…
Delete
Deletes matching entries in maps or arrays.
Note that versions prior to 4.18 require the 'eval/e' command to be specified.
yq e <exp> <file>

Delete entry in map

Given a sample.yml file of:
1
a: cat
2
b: dog
Copied!
then
1
yq 'del(.b)' sample.yml
Copied!
will output
1
a: cat
Copied!

Delete nested entry in map

Given a sample.yml file of:
1
a:
2
a1: fred
3
a2: frood
Copied!
then
1
yq 'del(.a.a1)' sample.yml
Copied!
will output
1
a:
2
a2: frood
Copied!

Delete entry in array

Given a sample.yml file of:
1
- 1
2
- 2
3
- 3
Copied!
then
1
yq 'del(.[1])' sample.yml
Copied!
will output
1
- 1
2
- 3
Copied!

Delete nested entry in array

Given a sample.yml file of:
1
- a: cat
2
b: dog
Copied!
then
1
yq 'del(.[0].a)' sample.yml
Copied!
will output
1
- b: dog
Copied!

Delete no matches

Given a sample.yml file of:
1
a: cat
2
b: dog
Copied!
then
1
yq 'del(.c)' sample.yml
Copied!
will output
1
a: cat
2
b: dog
Copied!

Delete matching entries

Given a sample.yml file of:
1
a: cat
2
b: dog
3
c: bat
Copied!
then
1
yq 'del( .[] | select(. == "*at") )' sample.yml
Copied!
will output
1
b: dog
Copied!

Recursively delete matching keys

Given a sample.yml file of:
1
a:
2
name: frog
3
b:
4
name: blog
5
age: 12
Copied!
then
1
yq 'del(.. | select(has("name")).name)' sample.yml
Copied!
will output
1
a:
2
b:
3
age: 12
Copied!