yq/pkg/yqlib/doc/operators/delete.md

121 lines
1.3 KiB
Markdown
Raw Normal View History

2021-11-03 04:00:58 +00:00
# Delete
Deletes matching entries in maps or arrays.
## Delete entry in map
Given a sample.yml file of:
```yaml
2023-05-30 05:05:28 +00:00
a: cat
b: dog
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq 'del(.b)' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
2023-05-30 05:05:28 +00:00
a: cat
2021-11-03 04:00:58 +00:00
```
## Delete nested entry in map
Given a sample.yml file of:
```yaml
2023-05-30 05:05:28 +00:00
a:
a1: fred
a2: frood
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq 'del(.a.a1)' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
2023-05-30 05:05:28 +00:00
a:
a2: frood
2021-11-03 04:00:58 +00:00
```
## Delete entry in array
Given a sample.yml file of:
```yaml
2023-05-30 05:05:28 +00:00
- 1
- 2
- 3
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq 'del(.[1])' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
2023-05-30 05:05:28 +00:00
- 1
- 3
2021-11-03 04:00:58 +00:00
```
## Delete nested entry in array
Given a sample.yml file of:
```yaml
2023-05-30 05:05:28 +00:00
- a: cat
b: dog
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq 'del(.[0].a)' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
2023-05-30 05:05:28 +00:00
- b: dog
2021-11-03 04:00:58 +00:00
```
## Delete no matches
Given a sample.yml file of:
```yaml
2023-05-30 05:05:28 +00:00
a: cat
b: dog
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq 'del(.c)' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
2023-05-30 05:05:28 +00:00
a: cat
b: dog
2021-11-03 04:00:58 +00:00
```
## Delete matching entries
Given a sample.yml file of:
```yaml
2023-05-30 05:05:28 +00:00
a: cat
b: dog
c: bat
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq 'del( .[] | select(. == "*at") )' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
2023-05-30 05:05:28 +00:00
b: dog
2021-11-03 04:00:58 +00:00
```
## Recursively delete matching keys
Given a sample.yml file of:
```yaml
2023-05-30 05:05:28 +00:00
a:
name: frog
b:
name: blog
age: 12
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq 'del(.. | select(has("name")).name)' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
2023-05-30 05:05:28 +00:00
a:
b:
age: 12
2021-11-03 04:00:58 +00:00
```