erlangelixirets

Unable to delete and select from an ets table


I am having trouble retrieving and deleting records from ETS.

After inserting them like:

:ets.insert(
  :table_name,
  {id, location, [name, x, z]}
)

I am unable to delete them like so:

:ets.delete(:table_name, id) 

I have also tried and this doesn't return anything.

:ets.match(:table_name, {id, location, '_'}) 

Solution

  • You always can review the content of :ets with :ets.foldl/3

    ets = :ets.new(:table_name, [])
    
    :ets.insert(ets, {1, 2, ["foo", 3, 4]})       
    #⇒ true
    
    :ets.foldl(&[&1|&2], [], ets)
    #⇒ [{1, 2, ["foo", 3, 4]}]
    

    :ets.delete/2 works out of the box:

    :ets.delete ets, 1
    #⇒ true
    

    For :ets.match/2 one should use atoms ( differs from in how to denote them) and specify what to return back.

    :ets.match(ets, {:_, :_, :'$1'})  
    #⇒ [[["foo", 3, 4]]]
    
    :ets.match(ets, {1, :'$1', :'$2'})
    #⇒ [[2, ["foo", 3, 4]]]