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.

Leave a Reply

Your email address will not be published. Required fields are marked *