# Event.remove not working

**URL:** <https://discuss.elastic.co/t/event-remove-not-working/134147>\
**Category:** Logstash\
**Created:** [June 1, 2018, 6:30am UTC](https://discuss.elastic.co/t/event-remove-not-working/134147 "2018-06-01T06:30:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akash\_Kumar\_Dutta](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@Akash\_Kumar\_Dutta](https://discuss.elastic.co/u/Akash_Kumar_Dutta)\
**Post date:** [June 1, 2018, 6:30am UTC](https://discuss.elastic.co/t/event-remove-not-working/134147/1 "2018-06-01T06:30:32Z")

</div>

I am using the following ruby filter to update the fields.

```
def filter(event)
  keys = event.to_hash.keys
  keys.each{|key|

    if ( key.start_with? '_' and key != "_@timestamp")
      value = event.get(key)
      newkey = key.sub!(/^_/, '')
      event.remove(key)
      event.set(newkey , value)

    elsif (key == "_@timestamp")
      value = event.get(key)
      event.set("@apptimestamp", Time.at(value))
      event.remove(key)
    end
      
  }
  return [event]

  rescue Exception => e
    event.set('logstash_ruby_exception', 'underscores: ' + e.message)
    return [event]
end

```

The code duplicates fields starting with underscore, to fields not starting with underscore. But for some reason doesn't eliminate the one's starting with underscore. That is, `event.remove` doesn't work as expected. Any ideas?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![guyboertje](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guyboertje/32/31592_2.png) [@guyboertje](https://discuss.elastic.co/u/guyboertje)\
**Post date:** [June 1, 2018, 8:31am UTC](https://discuss.elastic.co/t/event-remove-not-working/134147/2 "2018-06-01T08:31:22Z")

</div>

What version of Logstash?

---

<div class="post-metadata">

**Author:** ![Akash\_Kumar\_Dutta](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@Akash\_Kumar\_Dutta](https://discuss.elastic.co/u/Akash_Kumar_Dutta)\
**Post date:** [June 1, 2018, 8:33am UTC](https://discuss.elastic.co/t/event-remove-not-working/134147/3 "2018-06-01T08:33:21Z")

</div>

6.2.4  
image used is `docker.elastic.co/logstash/logstash:6.2.4`

---

<div class="post-metadata">

**Author:** ![guyboertje](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guyboertje/32/31592_2.png) [@guyboertje](https://discuss.elastic.co/u/guyboertje)\
**Post date:** [June 1, 2018, 10:05am UTC](https://discuss.elastic.co/t/event-remove-not-working/134147/4 "2018-06-01T10:05:38Z")

</div>

> [@Akash\_Kumar\_Dutta](#):
>
> newkey = key.sub!(/^\_/, '')

`sub!` alters the value of key so it is not removable.  
In Ruby IRB...

```auto
irb(main):001:0> key = "_abc"
=> "_abc"
irb(main):002:0> key.sub!(/^_/, "")
=> "abc"
irb(main):003:0> key
=> "abc"

```

Looking at how the mutate filter works and with my knowledge of ruby I suggest this improvement.

```auto
input {
  generator {
    message => '{"_a": "A", "_b": "B", "c_": "C"}'
    count => 1
  }
}

filter {
  json {
    source => "message"
  }
  if "_jsonparsefailure" not in [tags] {
    ruby {
      code => '
        keys = event.to_hash.keys
        keys.select{|key| key.start_with?("_") && key != "_@timestamp" }.each do |key|
          newkey = key[1, key.length] # we know the first character is an underscore
          event.set(newkey, event.remove(key))
        end
        # deal with _@timestamp here, not inside the iterator
      '
    }
  }
}

output {
  stdout {
    codec => rubydebug
  }
}

```

Gives

```auto
{
      "sequence" => 0,
             "a" => "A",
             "b" => "B",
    "@timestamp" => 2018-06-01T09:57:42.934Z,
      "@version" => "1",
          "host" => "Elastics-MacBook-Pro.local",
       "message" => "{\"_a\": \"A\", \"_b\": \"B\", \"c_\": \"C\"}",
            "c_" => "C"
}

```

---

<div class="post-metadata">

**Author:** ![Akash\_Kumar\_Dutta](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@Akash\_Kumar\_Dutta](https://discuss.elastic.co/u/Akash_Kumar_Dutta)\
**Post date:** [June 1, 2018, 12:57pm UTC](https://discuss.elastic.co/t/event-remove-not-working/134147/5 "2018-06-01T12:57:37Z")

</div>

Thank you so much! I am so silly, didn't notice that! 😃

---

<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:** [June 29, 2018, 12:57pm UTC](https://discuss.elastic.co/t/event-remove-not-working/134147/6 "2018-06-29T12:57:48Z")

</div>

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