How to delete a single instance from an :ets duplicate bag table?

I am working on :ets and using duplicate bag to store duplicate values.

cool_table =, [:duplicate_bag])
:ets.insert(cool_table, [{:a, 3}, {:a, 100}, {:a, 100}])
:ets.delete_object(cool_table, {:a, 100})
:ets.lookup(cool_table, :a) |> IO.inspect # => [a: 3]

Is there any way that only one instance of {:a, 100} gets deleted.


From the provided API I don’t think so. The documentation says that all instances of the object will be deleted. Hacking you could count the number of instances the table has, then delete and reinsert all but one.

You could count using Enum.count(:ets.match(cool_table, {:a, 100})) which will return an empty list for each entry matched: [[], []], since we don’t care about any values just how many, and then count.