2016-08-05 18:12:27 +02:00
|
|
|
package delete
|
|
|
|
|
|
|
|
import (
|
2019-06-17 10:34:30 +02:00
|
|
|
"context"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/cmd"
|
2020-04-29 13:15:30 +02:00
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs/operations"
|
2016-08-05 18:12:27 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2020-04-29 13:15:30 +02:00
|
|
|
var (
|
|
|
|
rmdirs = false
|
|
|
|
)
|
|
|
|
|
2016-08-05 18:12:27 +02:00
|
|
|
func init() {
|
2019-10-11 17:58:11 +02:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2020-04-29 13:15:30 +02:00
|
|
|
cmdFlags := commandDefinition.Flags()
|
|
|
|
flags.BoolVarP(cmdFlags, &rmdirs, "rmdirs", "", rmdirs, "rmdirs removes empty directories but leaves root intact")
|
2016-08-05 18:12:27 +02:00
|
|
|
}
|
|
|
|
|
2019-10-11 17:58:11 +02:00
|
|
|
var commandDefinition = &cobra.Command{
|
2016-08-05 18:12:27 +02:00
|
|
|
Use: "delete remote:path",
|
|
|
|
Short: `Remove the contents of path.`,
|
|
|
|
Long: `
|
2018-11-02 18:07:45 +01:00
|
|
|
Remove the files in path. Unlike ` + "`" + `purge` + "`" + ` it obeys include/exclude
|
2016-08-05 18:12:27 +02:00
|
|
|
filters so can be used to selectively delete files.
|
|
|
|
|
2018-11-02 18:07:45 +01:00
|
|
|
` + "`" + `rclone delete` + "`" + ` only deletes objects but leaves the directory structure
|
|
|
|
alone. If you want to delete a directory and all of its contents use
|
|
|
|
` + "`" + `rclone purge` + "`" + `
|
|
|
|
|
2020-04-29 13:15:30 +02:00
|
|
|
If you supply the --rmdirs flag, it will remove all empty directories along with it.
|
|
|
|
|
2016-08-05 18:12:27 +02:00
|
|
|
Eg delete all files bigger than 100MBytes
|
|
|
|
|
|
|
|
Check what would be deleted first (use either)
|
|
|
|
|
|
|
|
rclone --min-size 100M lsl remote:path
|
|
|
|
rclone --dry-run --min-size 100M delete remote:path
|
|
|
|
|
|
|
|
Then delete
|
|
|
|
|
|
|
|
rclone --min-size 100M delete remote:path
|
|
|
|
|
|
|
|
That reads "delete everything with a minimum size of 100 MB", hence
|
|
|
|
delete all files bigger than 100MBytes.
|
2020-06-05 18:04:23 +02:00
|
|
|
|
|
|
|
**Important**: Since this can cause data loss, test first with the
|
|
|
|
` + "`--dry-run` or the `--interactive`/`-i`" + ` flag.
|
2016-08-05 18:12:27 +02:00
|
|
|
`,
|
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
|
|
|
fsrc := cmd.NewFsSrc(args)
|
2016-12-04 17:52:24 +01:00
|
|
|
cmd.Run(true, false, command, func() error {
|
2020-04-29 13:15:30 +02:00
|
|
|
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
|
2016-08-05 18:12:27 +02:00
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|