title: Using ets:foldl as a poor man's forEach on every record
body: <p><strong>Short version: is it safe to use <code>ets:foldl</code> to delete every ETS record as one is iterating through them?</strong></p>
  
  <p>Suppose an ETS table is accumulating information and now it's time to process it all. A record is read from the table, used in some way, then deleted. (Also, assume the table is <code>private</code>, so no concurrency issues.)</p>
  
  <p>In another language, with a similar data structure, you might use a for...each loop, processing every record and then deleting it from the hash/dict/map/whatever. However, the <code>ets</code> module does not have <code>foreach</code> as e.g. <code>lists</code> does.</p>
  
  <p>But this might work:</p>
  
  <pre><code>1&gt; ets:new(ex, [named_table]).
  ex
  2&gt; ets:insert(ex, {alice, "high"}).
  true
  3&gt; ets:insert(ex, {bob, "medium"}).
  true
  4&gt; ets:insert(ex, {charlie, "low"}).
  true
  5&gt; ets:foldl(fun({Name, Adjective}, DontCare) -&gt;
        io:format("~p has a ~p opinion of you~n", [Name, Adjective]),
        ets:delete(ex, Name),
        DontCare
     end, notused, ex).
  bob has a "medium" opinion of you
  alice has a "high" opinion of you
  charlie has a "low" opinion of you
  notused
  6&gt; ets:info(ex).
  [...
   {size,0},
   ...]
  7&gt; ets:lookup(ex, bob).
  []
  </code></pre>
  
  <p><strong>Is this the preferred approach? Is it at least correct and bug-free?</strong></p>
  
  <p>I have a general concern about modifying a data structure while processing it, however the <a href="http://erldocs.com/R14B/stdlib/ets.html?i=13&amp;search=lookup#foldl/3" rel="nofollow">ets:foldl documentation</a> implies that ETS is pretty comfortable with you modifying records inside <code>foldl</code>. Since I am essentially wiping the table clean, I want to be sure.</p>
  
  <p>I am using Erlang R14B with a <code>set</code> table however I'd like to know if there are any caveats with any Erlang version, or any type of table as well. Thanks!</p>