# Ruby script for deleting some elements recursively \[Q&A\]

**URL:** https://discuss.elastic.co/t/ruby-script-for-deleting-some-elements-recursively-q-a/295253
**Category:** Logstash
**Created:** [January 24, 2022, 4:31pm UTC](https://discuss.elastic.co/t/ruby-script-for-deleting-some-elements-recursively-q-a/295253 "2022-01-24T16:31:24Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![SudoNova](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sudonova/32/82787_2.png) [@SudoNova](https://discuss.elastic.co/u/SudoNova)
#### Post date: [January 24, 2022, 4:31pm UTC](https://discuss.elastic.co/t/ruby-script-for-deleting-some-elements-recursively-q-a/295253/1 "2022-01-24T16:31:24Z")

</div>

Hi  
Just because I had troubles to make it work,I decided to share it maybe some other folks can use it some time.

**Requirement:** Delete all elements in a parsed json log where they are empty  
**Note:** Haproxy sends empty fields as `-`, that's why I used `eql("-")` so mind changing it if you need  
**Note:** I also delete empty arrays and hashes.

```auto
    ruby
    {
        code =>
        '

```

```ruby

            def recursion(event,hashed,path)
                hashed.keys.each do |k|
                    child = hashed["#{k}"]
                    if (child.is_a?(String) && child.eql?("-")) || (child.is_a?(Array) && child.empty?)
                        event.remove("#{path}[#{k}]")
                    elsif child.is_a?(Hash)
                        if child.empty?
                            event.remove("#{path}[#{k}]")
                        else
                            recursion event, child, "#{path}[#{k}]"
                        end
                    end
                end
            end
            begin
                recursion event, event.to_hash, ""
            end

```

```auto
        '
    }

```

---

<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 21, 2022, 4:32pm UTC](https://discuss.elastic.co/t/ruby-script-for-deleting-some-elements-recursively-q-a/295253/2 "2022-02-21T16:32:16Z")

</div>

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