From fc506f8538ddbd2519b46f65144eba71523a53f5 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sun, 21 Aug 2022 11:11:00 +0200 Subject: [PATCH] b2: Repeat deleting until all file versions are removed When hard deleting the latest file version on B2, this uncovers earlier versions. If an upload required retries, multiple version might exist for a file. Thus to reliably delete a file, we have to remove all versions of it. --- internal/backend/b2/b2.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/internal/backend/b2/b2.go b/internal/backend/b2/b2.go index 150d396d5..26e23627a 100644 --- a/internal/backend/b2/b2.go +++ b/internal/backend/b2/b2.go @@ -274,14 +274,22 @@ func (be *b2Backend) Remove(ctx context.Context, h restic.Handle) error { be.sem.GetToken() defer be.sem.ReleaseToken() - obj := be.bucket.Object(be.Filename(h)) - err := obj.Delete(ctx) - // consider a file as removed if b2 informs us that it does not exist - if b2.IsNotExist(err) { - return nil + // the retry backend will also repeat the remove method up to 10 times + for i := 0; i < 3; i++ { + obj := be.bucket.Object(be.Filename(h)) + err := obj.Delete(ctx) + if err == nil { + // keep deleting until we are sure that no leftover file versions exist + continue + } + // consider a file as removed if b2 informs us that it does not exist + if b2.IsNotExist(err) { + return nil + } + return errors.Wrap(err, "Delete") } - return errors.Wrap(err, "Delete") + return errors.New("failed to delete all file versions") } type semLocker struct {