From 21964f90fdb58f74ac3112fc95494d85ae7c5c2c Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Wed, 17 Jul 2019 17:01:44 +0200 Subject: [PATCH] etcd: add DeletePrefix method --- internal/etcd/etcd.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/internal/etcd/etcd.go b/internal/etcd/etcd.go index ca3dae7..84fdba7 100644 --- a/internal/etcd/etcd.go +++ b/internal/etcd/etcd.go @@ -286,6 +286,22 @@ func (s *Store) Delete(ctx context.Context, key string) error { return err } +func (s *Store) DeletePrefix(ctx context.Context, prefix string) error { + etcdv3Options := []clientv3.OpOption{} + + key := prefix + if len(key) == 0 { + key = "\x00" + etcdv3Options = append(etcdv3Options, clientv3.WithFromKey()) + } else { + etcdv3Options = append(etcdv3Options, clientv3.WithPrefix()) + } + + _, err := s.c.Delete(ctx, key, etcdv3Options...) + + return err +} + func (s *Store) AtomicDelete(ctx context.Context, key string, revision int64) (*etcdclientv3.TxnResponse, error) { cmp := etcdclientv3.Compare(etcdclientv3.ModRevision(key), "=", revision) req := etcdclientv3.OpDelete(key)