Delete all riak DB buckets

The riak erlang client does not offer a function call that deletes all buckets. The erlang client only allows to remove empty buckets, that means they does not contain any keys. This should prevent you from accidentally delete all your data stored in the riak db.
But during the development it can be handy to reset the DB. I found some solutions for different languages:

But i missed a module for erlang, so here is my try. First step is to find all keys in a given bucket and delete them.

deleteAllKeys(Pid, Bucket, Keys) -> 
    deleteAllKeys(Pid, Bucket, Keys, []).
deleteAllKeys(_, _, [], Acc) ->
    {ok, Acc};
deleteAllKeys(Pid, Bucket, [Key|Rest], Acc) ->
    riakc_pb_socket:delete(Pid,Bucket,Key),
    deleteAllKeys(Pid,Bucket,Rest, Acc++Key).

This function is used during the iteration through all buckets

deleteBucket(Pid, Buckets) -> 
    deleteBucket(Pid, Buckets, []).
deleteBucket(_, [], Acc) ->
    {ok, Acc};
deleteBucket(Pid, [Bucket|RestBucket], Acc) ->
    {ok, Keys} = riakc_pb_socket:list_keys(Pid, Bucket),
    {ok, _} = deleteAllKeys(Pid,Bucket, Keys),
    deleteBucket(Pid, RestBucket, Acc++[Bucket]).

the complete module can be found on Github. Have fun and feel free to give feedback.

Create a supervised erlang application with rebar in minutes

create a erlang application with OTP structure. first we need rebar, off cores. I highly recommend to put the created rebar binary into one of your paths. If you want to play with erlang, you will need rebar quite often.

first create a directory

$ mkdir sample
$ cd sample

create the Erlang OTP Application

$ rebar create-app appid=sample
==> sample (create-app)
Writing src/sample.app.src
Writing src/sample_app.erl
Writing src/sample_sup.erl
$

and now it can be compiled. go for it.

$ rebar compile

create a new rebar config file in your application root directory called rebar.config

$echo '{sub_dirs, ["rel"]}.' > rebar.config

make a release

$ mkdir rel
$ cd rel
$ rebar create-node nodeid=sample

change the default rel/reltool.config file

$ {lib_dirs, []} -> {lib_dirs, ["../../"]}

in the application root directory we can generate a release.

$ rebar generate

done. Now start the erlang application with

$ rel/sample/bin/sample start

and stop it if you want with

$ rel/sample/bin/sample stop
ok