Delete

yq d <yaml_file> <path_to_delete>

To Stdout

Given a sample.yaml file of:

b:
  c: 2
  apples: green

then

yq d sample.yaml b.c

will output:

b:
  apples: green

From STDIN

cat sample.yaml | yq d - b.c

Deleting array elements

Given a sample.yaml file of:

b:
  c: 
    - 1
    - 2
    - 3

then

yq d sample.yaml 'b.c[1]'

will output:

b:
  c:
  - 1
  - 3

Deleting nodes in-place

Given a sample.yaml file of:

b:
  c: 2
  apples: green

then

yq d -i sample.yaml b.c

will update the sample.yaml file so that the 'c' node is deleted

Splat

Given a sample.yaml file of:

---
bob:
  item1:
    cats: bananas
    dogs: woof
  item2:
    cats: apples
    dogs: woof2
  thing:
    cats: oranges
    dogs: woof3

then

yq d sample.yaml bob.*.cats

will output:

---
bob:
  item1:
    dogs: woof
  item2:
    dogs: woof2
  thing:
    dogs: woof3

Prefix Splat

Given a sample.yaml file of:

---
bob:
  item1:
    cats: bananas
    dogs: woof
  item2:
    cats: apples
    dogs: woof2
  thing:
    cats: oranges
    dogs: woof3

then

yq d sample.yaml bob.item*.cats

will output:

---
bob:
  item1:
    dogs: woof
  item2:
    dogs: woof2
  thing:
    cats: oranges
    dogs: woof3

Array Splat

Given a sample.yaml file of:

---
bob:
- cats: bananas
  dogs: woof
- cats: apples
  dogs: woof2
- cats: oranges
  dogs: woof3

then

yq d sample.yaml bob.[*].cats

will output:

---
bob:
- dogs: woof
- dogs: woof2
- dogs: woof3

Multiple Documents - delete from single document

Given a sample.yaml file of:

something: else
field: leaveMe
---
b:
  c: 2
field: deleteMe

then

yq w -d1 sample.yaml field

will output:

something: else
field: leaveMe
---
b:
  c: 2

Multiple Documents - delete from all documents

Given a sample.yaml file of:

something: else
field: deleteMe
---
b:
  c: 2
field: deleteMeToo

then

yq w -d'*' sample.yaml field

will output:

something: else
---
b:
  c: 2

Note that '*' is in quotes to avoid being interpreted by your shell.

Keys with dots

When specifying a key that has a dot use key lookup indicator.

b:
  foo.bar: 7
yaml r sample.yaml 'b[foo.bar]'
yaml w sample.yaml 'b[foo.bar]' 9

Any valid yaml key can be specified as part of a key lookup.

Note that the path is in quotes to avoid the square brackets being interpreted by your shell.

Keys (and values) with leading dashes

If a key or value has leading dashes, yq won't know that you are passing a value as opposed to a flag (and you will get a 'bad flag syntax' error).

To fix that, you will need to tell it to stop processing flags by adding '--' after the last flag like so:

yq n -t -- --key --value

Will result in

` --key: --value

Last updated