deleting records from mnesia

Ulf Wiger (TN/EAB) ulf.wiger@REDACTED
Wed Jul 19 19:28:37 CEST 2006


I tried with my slightly patched version:

(tups1@REDACTED)2> mnesia:create_table(test,[]). 
{atomic,ok}
(tups1@REDACTED)3> [mnesia:dirty_write({test,I,v}) || I <-
lists:seq(1,10)].
[ok,ok,ok,ok,ok,ok,ok,ok,ok,ok]
(tups1@REDACTED)4> ets:tab2list(test).
[{test,1,v},
 {test,10,v},
 {test,2,v},
 {test,3,v},
 {test,8,v},
 {test,4,v},
 {test,5,v},
 {test,7,v},
 {test,6,v},
 {test,9,v}]
(tups1@REDACTED)5> mnesia_schema:schema_transaction(
  fun() -> mnesia_schema:do_clear_table(test),
           mnesia:write({test,1,a}) end).
{atomic,ok}
(tups1@REDACTED)6> ets:tab2list(test).
[{test,1,a}]


The only change that's required is that 
mnesia_schema:do_clear_table/1 is exported.

BR,
Ulf W
           

> -----Original Message-----
> From: owner-erlang-questions@REDACTED 
> [mailto:owner-erlang-questions@REDACTED] On Behalf Of Serge 
> Aleynikov
> Sent: den 19 juli 2006 18:17
> To: Erlang Users' List
> Subject: deleting records from mnesia
> 
> Hi,
> 
> Is there a way to delete all records from a table in mnesia 
> within a transaction?
> 
> This won't work because mnesia:clean_table/1 doesn't support nested
> transactions:
> 
> F = fun() ->
>          {atomic, ok} = mnesia:clear_table(Tab),
>          insert_records(Tab)
>      end,
> mnesia:transaction(F).
> 
> I know that I could do this:
> 
> [mnesia:delete(Tab, Key, sticky_write) || Key <- 
> mnesia:all_keys(Tab)].
> 
> but is there a way to truncate table in one shot within a 
> transaction instead of iterating over all records?
> 
> Serge
> 



More information about the erlang-questions mailing list