# Delete value from filed in recursive way

**URL:** https://discuss.elastic.co/t/delete-value-from-filed-in-recursive-way/294088
**Category:** Logstash
**Created:** [January 11, 2022, 10:06pm UTC](https://discuss.elastic.co/t/delete-value-from-filed-in-recursive-way/294088 "2022-01-11T22:06:28Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![INS](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ins/32/92827_2.png) [@INS](https://discuss.elastic.co/u/INS)
#### Post date: [January 11, 2022, 10:06pm UTC](https://discuss.elastic.co/t/delete-value-from-filed-in-recursive-way/294088/1 "2022-01-11T22:06:28Z")

</div>

Hi  
I need to remove from a few fields in the recursive way some value so I've tried in two steps but without success, can You check what's wrong

**input data:**

```auto
"ue_stat" => "undefined-undefined",
 "s_gw" => "122.123.205.100-undefined",
 "psm_info2_lte" => "undefined-undefined",
"psm_info_lte" => "undefined-undefined-undefined-undefined"

```

================================

```auto

   ruby {
    code => "
      hash = event.to_hash
      hash.each do |k,v|
        if v == 'undefined-'
          event.remove(v)
        end
      end
    "
  }
  
  
  
   ruby {
    code => "
      hash = event.to_hash
      hash.each do |k,v|
        if v == '[-]*undefined'
          event.remove(v)
        end
      end
    "
  }

```

---

<div class="post-metadata">

### Author: ![INS](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ins/32/92827_2.png) [@INS](https://discuss.elastic.co/u/INS)
#### Post date: [January 12, 2022, 11:37am UTC](https://discuss.elastic.co/t/delete-value-from-filed-in-recursive-way/294088/2 "2022-01-12T11:37:48Z")

</div>

today I've tried also the new one code but get the same result. I don't know where the is the root cause.

```auto
 ruby {
    code => "
    
    def stripit(value)
    value.gsub!('[-]*undefined','')
    value
    end
    
      hash = event.to_hash
      hash.each do |k,value|
        if value == '[-]*undefined'
          stripit(value)
        end
      end
    "
  }

  ruby {
    code => "
    
    def stripito(value)
    value.gsub!('undefined-','')
    value
    end
    
      hash = event.to_hash
      hash.each do |k,value|
        if value == 'undefined-'
          stripito(value)
        end
      end
    "
}

```

---

<div class="post-metadata">

### Author: ![INS](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ins/32/92827_2.png) [@INS](https://discuss.elastic.co/u/INS)
#### Post date: [January 12, 2022, 12:52pm UTC](https://discuss.elastic.co/t/delete-value-from-filed-in-recursive-way/294088/3 "2022-01-12T12:52:31Z")

</div>

also this approach doesn't work

```auto
  ruby {
    code => "  
    hash = event.to_hash
      hash.each do |k,v|
        if v == '[-]*undefined'
           event.set(k, v.gsub!('[-]*undefined',''))
        end
      end
    "
  }

  ruby {
    code => "
      hash = event.to_hash
      hash.each do |k,v|
        if v == 'undefined-'
           event.set(k, v.gsub!('undefined-',''))
        end
      end
    "
  }

```

@Badger Can You keep an eye? on this -\>  
Do You have any idea? Please note that all these fields are strings

---

<div class="post-metadata">

### Author: ![INS](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ins/32/92827_2.png) [@INS](https://discuss.elastic.co/u/INS)
#### Post date: [January 12, 2022, 1:46pm UTC](https://discuss.elastic.co/t/delete-value-from-filed-in-recursive-way/294088/4 "2022-01-12T13:46:26Z")

</div>

> [@INS](#):
>
> `event.set(k, v.gsub`

I found that the problem can be in regex but the same one regex works in

```auto
	mutate {
		gsub => ["ue_stat","undefined-",""]
	}

	mutate {
		gsub => ["ue_stat","[-]*undefined",""]
	}

```

if we put it into this method (event.set) there is a problem. But logstash doesn't spit out any bugs.

---

<div class="post-metadata">

### Author: ![INS](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ins/32/92827_2.png) [@INS](https://discuss.elastic.co/u/INS)
#### Post date: [January 13, 2022, 2:44pm UTC](https://discuss.elastic.co/t/delete-value-from-filed-in-recursive-way/294088/5 "2022-01-13T14:44:29Z")

</div>

ok now it's clear ruby has a problem with regex so I user another method  
" Using include? method from string"

```
ruby {
code => "
  hash = event.to_hash
  hash.each do |k,v|
  if v.is_a? String 
   if v.include? 'undefined-'
    event.set(k, v.gsub!('undefined-',''))
    end
   end
  end
"

```

}

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [February 10, 2022, 2:45pm UTC](https://discuss.elastic.co/t/delete-value-from-filed-in-recursive-way/294088/6 "2022-02-10T14:45:28Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
