-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete.go
76 lines (61 loc) · 1.4 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"fmt"
"log"
"google.golang.org/api/iterator"
)
func delete(arguments []string) {
if client == nil {
fmt.Printf("not connected - use %q\n", "connect")
return
}
for _, id := range arguments {
_, err := client.Collection(collection).Doc(id).Delete(ctx)
if err != nil {
// Handle any errors in an appropriate way, such as returning them.
log.Printf("An error has occurred: %s", err)
}
fmt.Printf("Document with id %q deleted\n", id)
}
}
func deleteall(arguments []string) {
if client == nil {
fmt.Printf("not connected - use %q\n", "connect")
return
}
ref := client.Collection(collection)
batchSize := 20
for {
// Get a batch of documents
iter := ref.Limit(batchSize).Documents(ctx)
numDeleted := 0
// Iterate through the documents, adding
// a delete operation for each one to a
// WriteBatch.
batch := client.Batch()
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
fmt.Printf("failed next iteration: %v\n", err)
return
}
batch.Delete(doc.Ref)
numDeleted++
}
// If there are no documents to delete,
// the process is over.
if numDeleted == 0 {
fmt.Printf("all documents deleted\n")
return
}
result, err := batch.Commit(ctx)
if err != nil {
fmt.Printf("failed to commit batch for deletion: %v\n", err)
return
}
fmt.Printf("Commit Result: %v\n", result)
}
}