2021-10-30 03:14:39 +00:00
|
|
|
# Delete
|
|
|
|
|
|
|
|
Deletes matching entries in maps or arrays.
|
|
|
|
|
2022-02-06 03:41:27 +00:00
|
|
|
{% hint style="warning" %}
|
|
|
|
Note that versions prior to 4.18 require the 'eval/e' command to be specified. 
|
|
|
|
|
|
|
|
`yq e <exp> <file>`
|
|
|
|
{% endhint %}
|
|
|
|
|
2021-10-30 03:14:39 +00:00
|
|
|
## Delete entry in map
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: cat
|
|
|
|
b: dog
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-28 01:45:43 +00:00
|
|
|
yq 'del(.b)' sample.yml
|
2021-10-30 03:14:39 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a: cat
|
|
|
|
```
|
|
|
|
|
|
|
|
## Delete nested entry in map
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
a1: fred
|
|
|
|
a2: frood
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-28 01:45:43 +00:00
|
|
|
yq 'del(.a.a1)' sample.yml
|
2021-10-30 03:14:39 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
a2: frood
|
|
|
|
```
|
|
|
|
|
|
|
|
## Delete entry in array
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
- 1
|
|
|
|
- 2
|
|
|
|
- 3
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-28 01:45:43 +00:00
|
|
|
yq 'del(.[1])' sample.yml
|
2021-10-30 03:14:39 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
- 1
|
|
|
|
- 3
|
|
|
|
```
|
|
|
|
|
|
|
|
## Delete nested entry in array
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
- a: cat
|
|
|
|
b: dog
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-28 01:45:43 +00:00
|
|
|
yq 'del(.[0].a)' sample.yml
|
2021-10-30 03:14:39 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
- b: dog
|
|
|
|
```
|
|
|
|
|
|
|
|
## Delete no matches
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: cat
|
|
|
|
b: dog
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-28 01:45:43 +00:00
|
|
|
yq 'del(.c)' sample.yml
|
2021-10-30 03:14:39 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a: cat
|
|
|
|
b: dog
|
|
|
|
```
|
|
|
|
|
|
|
|
## Delete matching entries
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: cat
|
|
|
|
b: dog
|
|
|
|
c: bat
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-28 01:45:43 +00:00
|
|
|
yq 'del( .[] | select(. == "*at") )' sample.yml
|
2021-10-30 03:14:39 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
b: dog
|
|
|
|
```
|
|
|
|
|
|
|
|
## Recursively delete matching keys
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
name: frog
|
|
|
|
b:
|
|
|
|
name: blog
|
|
|
|
age: 12
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-28 01:45:43 +00:00
|
|
|
yq 'del(.. | select(has("name")).name)' sample.yml
|
2021-10-30 03:14:39 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
b:
|
|
|
|
age: 12
|
|
|
|
```
|
2021-11-03 04:00:28 +00:00
|
|
|
|