From f555873f1878030dd334aa4ebef28248ecc02eed Mon Sep 17 00:00:00 2001 From: Kush Date: Wed, 29 Apr 2020 16:45:30 +0530 Subject: [PATCH] delete: added --rmdirs flag to delete directories as well - fixes #4055 If you supply the --rmdirs flag with delete command, it will remove all empty directories along with it leaving the root intact. --- cmd/delete/delete.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/cmd/delete/delete.go b/cmd/delete/delete.go index 0e9f5b3da..bf3c0fa81 100644 --- a/cmd/delete/delete.go +++ b/cmd/delete/delete.go @@ -4,12 +4,19 @@ import ( "context" "github.com/rclone/rclone/cmd" + "github.com/rclone/rclone/fs/config/flags" "github.com/rclone/rclone/fs/operations" "github.com/spf13/cobra" ) +var ( + rmdirs = false +) + func init() { cmd.Root.AddCommand(commandDefinition) + cmdFlags := commandDefinition.Flags() + flags.BoolVarP(cmdFlags, &rmdirs, "rmdirs", "", rmdirs, "rmdirs removes empty directories but leaves root intact") } var commandDefinition = &cobra.Command{ @@ -23,6 +30,8 @@ filters so can be used to selectively delete files. alone. If you want to delete a directory and all of its contents use ` + "`" + `rclone purge` + "`" + ` +If you supply the --rmdirs flag, it will remove all empty directories along with it. + Eg delete all files bigger than 100MBytes Check what would be deleted first (use either) @@ -41,7 +50,14 @@ delete all files bigger than 100MBytes. cmd.CheckArgs(1, 1, command, args) fsrc := cmd.NewFsSrc(args) cmd.Run(true, false, command, func() error { - return operations.Delete(context.Background(), fsrc) + if err := operations.Delete(context.Background(), fsrc); err != nil { + return err + } + if rmdirs { + fdst := cmd.NewFsDir(args) + return operations.Rmdirs(context.Background(), fdst, "", true) + } + return nil }) }, }