# How to run a ruby function that updates and event and clears the empty fields recursively

**URL:** https://discuss.elastic.co/t/how-to-run-a-ruby-function-that-updates-and-event-and-clears-the-empty-fields-recursively/326057
**Category:** Logstash
**Created:** [February 21, 2023, 11:07am UTC](https://discuss.elastic.co/t/how-to-run-a-ruby-function-that-updates-and-event-and-clears-the-empty-fields-recursively/326057 "2023-02-21T11:07:23Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![vilman](https://avatars.discourse-cdn.com/v4/letter/v/51bf81/32.png) [@vilman](https://discuss.elastic.co/u/vilman)
#### Post date: [February 21, 2023, 11:07am UTC](https://discuss.elastic.co/t/how-to-run-a-ruby-function-that-updates-and-event-and-clears-the-empty-fields-recursively/326057/1 "2023-02-21T11:07:23Z")

</div>

I got an event which sometimes contain empty fields. I would like to delete those fields which are null and those which are empty.

---

<div class="post-metadata">

### Author: ![vilman](https://avatars.discourse-cdn.com/v4/letter/v/51bf81/32.png) [@vilman](https://discuss.elastic.co/u/vilman)
#### Post date: [February 21, 2023, 11:11am UTC](https://discuss.elastic.co/t/how-to-run-a-ruby-function-that-updates-and-event-and-clears-the-empty-fields-recursively/326057/2 "2023-02-21T11:11:56Z")

</div>

Check this code, works pretty good.

This avoids needing to use LogStash::Event stuff which overcomplicates things. Then you just need to pass the event to the recursive function.

```auto
          def delete_unknowns_from_hash(event, parents, new_hash)
            new_hash.each { |k, v|                  
              if v.is_a? String and ( v == "unknown" || v =="" ) then
                event.remove(parents + "[#{k}]")
              elsif v == nil then
                event.remove(parents + "[#{k}]")
              elsif v.is_a? Hash then
                delete_unknowns_from_hash(event, parents + "[#{k}]", v)
              end
            }
          end

          delete_unknowns_from_hash(event,"",event.to_hash)

```

---

<div class="post-metadata">

### Author: ![vilman](https://avatars.discourse-cdn.com/v4/letter/v/51bf81/32.png) [@vilman](https://discuss.elastic.co/u/vilman)
#### Post date: [February 21, 2023, 11:12am UTC](https://discuss.elastic.co/t/how-to-run-a-ruby-function-that-updates-and-event-and-clears-the-empty-fields-recursively/326057/3 "2023-02-21T11:12:30Z")

</div>

Beautiful! Thanks! 😄

---

<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: [March 21, 2023, 11:12am UTC](https://discuss.elastic.co/t/how-to-run-a-ruby-function-that-updates-and-event-and-clears-the-empty-fields-recursively/326057/4 "2023-03-21T11:12:34Z")

</div>

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