mirror of
https://github.com/rclone/rclone.git
synced 2024-11-22 16:34:30 +01:00
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.
This commit is contained in:
parent
1c8eab81a5
commit
f555873f18
@ -4,12 +4,19 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/rclone/rclone/cmd"
|
"github.com/rclone/rclone/cmd"
|
||||||
|
"github.com/rclone/rclone/fs/config/flags"
|
||||||
"github.com/rclone/rclone/fs/operations"
|
"github.com/rclone/rclone/fs/operations"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
rmdirs = false
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmd.Root.AddCommand(commandDefinition)
|
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{
|
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
|
alone. If you want to delete a directory and all of its contents use
|
||||||
` + "`" + `rclone purge` + "`" + `
|
` + "`" + `rclone purge` + "`" + `
|
||||||
|
|
||||||
|
If you supply the --rmdirs flag, it will remove all empty directories along with it.
|
||||||
|
|
||||||
Eg delete all files bigger than 100MBytes
|
Eg delete all files bigger than 100MBytes
|
||||||
|
|
||||||
Check what would be deleted first (use either)
|
Check what would be deleted first (use either)
|
||||||
@ -41,7 +50,14 @@ delete all files bigger than 100MBytes.
|
|||||||
cmd.CheckArgs(1, 1, command, args)
|
cmd.CheckArgs(1, 1, command, args)
|
||||||
fsrc := cmd.NewFsSrc(args)
|
fsrc := cmd.NewFsSrc(args)
|
||||||
cmd.Run(true, false, command, func() error {
|
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
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user