Skip to content
immudb docker run -d --net host -it --name immudb codenotary/immudb:latest

Deleting

It’s possible to achieve deletion by using the Delete function. It provides logical deletion, it means that it is not physically deleted from db, but it’s not possible to query it anymore after deletion. When immudb is used as an embedded store, it’s possible to retrieve deleted entries. It’s also possible to see deleted entries from the sdks using History endpoint, it will display if the entry was deleted.

Go

 1package main
 2
 3import (
 4	"context"
 5	"fmt"
 6	"log"
 7	"strings"
 8
 9	"github.com/codenotary/immudb/pkg/api/schema"
10	immudb "github.com/codenotary/immudb/pkg/client"
11)
12
13func main() {
14	opts := immudb.DefaultOptions().
15		WithAddress("localhost").
16		WithPort(3322)
17
18	client := immudb.NewClient().WithOptions(opts)
19	err := client.OpenSession(
20		context.TODO(),
21		[]byte(`immudb`),
22		[]byte(`immudb`),
23		"defaultdb",
24	)
25	if err != nil {
26		log.Fatal(err)
27	}
28
29	defer client.CloseSession(context.TODO())
30
31	tx, err := client.Set(
32		context.TODO(),
33		[]byte("1,2,3"),
34		[]byte("3,2,1"),
35	)
36	if err != nil {
37		log.Fatal(err)
38	}
39
40	fmt.Printf("Successfully committed tx %d\n", tx.Id)
41
42	entry, err := client.Get(
43		context.TODO(),
44		[]byte("1,2,3"),
45	)
46	if err != nil {
47		log.Fatal(err)
48	}
49
50	fmt.Printf("Successfully retrieved entry: %v\n", entry)
51
52	_, err = client.Delete(context.TODO(), &schema.DeleteKeysRequest{
53		Keys: [][]byte{
54			[]byte("1,2,3"),
55		},
56	})
57	if err != nil {
58		log.Fatal(err)
59	}
60
61	// the following will raise an error with key not found
62	_, err = client.Get(
63		context.TODO(),
64		[]byte("1,2,3"),
65	)
66	if err == nil || !strings.Contains(err.Error(), "key not found") {
67		log.Fatalf("expecting key not found error: %v", err)
68	}
69}

Java

 1package io.codenotary.immudb.helloworld;
 2
 3import io.codenotary.immudb4j.FileImmuStateHolder;
 4import io.codenotary.immudb4j.ImmuClient;
 5import io.codenotary.immudb4j.exceptions.KeyNotFoundException;
 6
 7public class App {
 8
 9    public static void main(String[] args) {
10
11        ImmuClient client = null;
12
13        try {
14
15            FileImmuStateHolder stateHolder = FileImmuStateHolder.newBuilder()
16                    .withStatesFolder("./immudb_states")
17                    .build();
18
19            client = ImmuClient.newBuilder()
20                    .withServerUrl("127.0.0.1")
21                    .withServerPort(3322)
22                    .withStateHolder(stateHolder)
23                    .build();
24
25            client.openSession("defaultdb", "immudb", "immudb");
26
27            byte[] value1 = { 0, 1, 2, 3 };
28
29            client.set("key1", value1);
30
31            client.delete("key1");
32
33            try {
34                client.get("key1");
35                throw new RuntimeException("key not found expected");
36            } catch (KeyNotFoundException e) {
37                // exception is expected
38            }
39
40            client.closeSession();
41
42        } catch (Exception e) {
43            e.printStackTrace();
44        } finally {
45            if (client != null) {
46                try {
47                    client.shutdown();
48                } catch (InterruptedException e) {
49                    e.printStackTrace();
50                }
51            }
52        }
53
54    }
55
56}

.NET

 1
 2var client = ImmuClient.NewBuilder().WithServerUrl(immudbServerAddress).Build();
 3await client.Open("immudb", "immudb", "defaultdb");
 4
 5string key = "hello";
 6
 7try
 8{
 9    await client.VerifiedSet(key, "immutable world!");
10    await client.Delete(key);
11}
12catch (VerificationException e)
13{
14    // VerificationException means Data Tampering detected!
15    // This means the history of changes has been tampered.
16    Console.WriteLine(e.ToString());
17}
18await client.Close();

Python

 1from immudb import ImmudbClient
 2from immudb.datatypes import DeleteKeysRequest
 3
 4URL = "localhost:3322"  # immudb running on your machine
 5LOGIN = "immudb"        # Default username
 6PASSWORD = "immudb"     # Default password
 7DB = b"defaultdb"       # Default database name (must be in bytes)
 8
 9def main():
10    client = ImmudbClient(URL)
11    client.login(LOGIN, PASSWORD, database = DB)
12    client.set(b"immu", b"immudb-not-rulezz")
13    print(client.get(b"immu"))  # b"immudb-not-rulezz"
14
15    deleteRequest = DeleteKeysRequest(keys = [b"immu"])
16    client.delete(deleteRequest)
17    print(client.get(b"immu"))  # None
18
19if __name__ == "__main__":
20    main()

Node.js

This feature is not yet supported or not documented. Do you want to make a feature request or help out? Open an issue on Node.js sdk github project

Others

If you’re using another development language, please refer to the immugw option.

Edit this page on GitHub Last updated