<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><span style="background-color: rgba(255, 255, 255, 0);">From: ?? <<a dir="ltr" href="mailto:zhuo.quan@outlook.com" x-apple-data-detectors="true" x-apple-data-detectors-type="link" x-apple-data-detectors-result="6">zhuo.quan@outlook.com</a>><br>To: Erlang???? <<a dir="ltr" href="mailto:erlang-questions@erlang.org" x-apple-data-detectors="true" x-apple-data-detectors-type="link" x-apple-data-detectors-result="7">erlang-questions@erlang.org</a>><br>Subject: [erlang-questions] How to delete records in a mnesia table<br>   when    traverse it<br>Message-ID: <<a dir="ltr" href="mailto:SNT149-W742CC9A17AD0BBCAECDAA5F1170@phx.gbl" x-apple-data-detectors="true" x-apple-data-detectors-type="link" x-apple-data-detectors-result="8">SNT149-W742CC9A17AD0BBCAECDAA5F1170@phx.gbl</a>><br>Content-Type: text/plain; charset="gb2312"<br><br></span><blockquote type="cite"><span style="background-color: rgba(255, 255, 255, 0);">Hi, list:<br><br> I have a mnesia table about 15K records in my application. As the time goes, I find that some records(10K) in the table is useless. So I want to delete these useless items with a piece of code like this:<br><br>Here is the code:<br>%%%%%%%%%%%%%%%%%%%%%<br>1.   delete() -><br>2.        traversal_tab(tab).<br><br>3.   traversal_tab(TabName) -><br>4.       traversal_tab(TabName, mnesia:dirty_first(TabName)) .<br><br>5.  traversal_tab(TabName, '$end_of_table') -><br>6.       io:format("finished~n");<br>7.  traversal_tab(TabName, Key) -><br>8.       delete_entry(TabName, Key),<br>9.       traversal_tab(TabName, mnesia:dirty_next(TabName, Key)).<br><br>10.  delete_entry(TabName, Key) -><br>11.      if ... <br>            %% delete the record if it fulfils the condition.<br>12.        mnesia:dirty_delete(TabName, Key)<br>              ...13.       end.<br>%%%%%%%%%%%%%%%%%%%%%%%%<br><br>The problem is if I delete the certain record in line 12,  the function mnesia:dirty_next(TabName, Key) in line 9 will fail.<br>How to delete records when i traverse a mnesia table? Anyone Help? </span><br><br></blockquote><br></div><div><br></div><div>Depending on your criteria, maybe you could use dirty-match or dirty-select to collect the records that need to be deleted in one pass,  and then delete those objects by iterating though that collection. </div><div><br></div><div>Alternately, you could have a process that (at regular intervals) walks the table marking records for deletion (by setting a field reserved for this purpose) that meet your criteria and then deletes them as described above. </div></body></html>