Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

b2: cleanup old file versions - fixes #462

Nick Craig-Wood018fe80bcb8e
diff --git a/b2/b2.go b/b2/b2.go
index 5897433ad..ee5920972 100644
--- a/b2/b2.go
+++ b/b2/b2.go
@@ -749,10 +749,12 @@ func (f *Fs) deleteByID(ID, Name string) error {
return nil
}
-// Purge deletes all the files and directories
+// purge deletes all the files and directories
+//
+// if oldOnly is true then it deletes only non current files.
//
// Implemented here so we can make sure we delete old versions.
-func (f *Fs) Purge() error {
+func (f *Fs) purge(oldOnly bool) error {
var errReturn error
var checkErrMutex sync.Mutex
var checkErr = func(err error) {
@@ -774,24 +776,46 @@ func (f *Fs) Purge() error {
go func() {
defer wg.Done()
for object := range toBeDeleted {
+ fs.Stats.Transferring(object.Name)
checkErr(f.deleteByID(object.ID, object.Name))
+ fs.Stats.DoneTransferring(object.Name)
}
}()
}
+ last := ""
checkErr(f.list("", fs.MaxLevel, "", 0, true, func(remote string, object *api.File, isDirectory bool) error {
if !isDirectory {
- fs.Debug(remote, "Deleting (id %q)", object.ID)
- toBeDeleted <- object
+ fs.Stats.Checking(remote)
+ if oldOnly && last != remote {
+ fs.Debug(remote, "Not deleting current version (id %q) %q", object.ID, object.Action)
+ } else {
+ fs.Debug(remote, "Deleting (id %q)", object.ID)
+ toBeDeleted <- object
+ }
+ last = remote
+ fs.Stats.DoneChecking(remote)
}
return nil
}))
close(toBeDeleted)
wg.Wait()
- checkErr(f.Rmdir())
+ if !oldOnly {
+ checkErr(f.Rmdir())
+ }
return errReturn
}
+// Purge deletes all the files and directories including the old versions.
+func (f *Fs) Purge() error {
+ return f.purge(false)
+}
+
+// CleanUp deletes all the hidden files.
+func (f *Fs) CleanUp() error {
+ return f.purge(true)
+}
+
// Hashes returns the supported hash sets.
func (f *Fs) Hashes() fs.HashSet {
return fs.HashSet(fs.HashSHA1)
@@ -1257,7 +1281,8 @@ func (o *Object) Remove() error {
// Check the interfaces are satisfied
var (
- _ fs.Fs = &Fs{}
- _ fs.Purger = &Fs{}
- _ fs.Object = &Object{}
+ _ fs.Fs = &Fs{}
+ _ fs.Purger = &Fs{}
+ _ fs.CleanUpper = &Fs{}
+ _ fs.Object = &Object{}
)
diff --git a/docs/content/b2.md b/docs/content/b2.md
index e08ac4898..f09ceea77 100644
--- a/docs/content/b2.md
+++ b/docs/content/b2.md
@@ -119,9 +119,11 @@ available.
The old versions of files are visible in the B2 web interface, but not
via rclone yet.
-Rclone doesn't provide any way of managing old versions (downloading
-them or deleting them) at the moment. When you `purge` a bucket, all
-the old versions will be deleted.
+If you wish to remove all the old versions then you can use the
+`rclone cleanup remote:bucket` command which will delete all the old
+versions of files, leaving the current ones intact.
+
+When you `purge` a bucket, all the old versions will be deleted also.
### Transfers ###