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",
|
2020-11-13 11:07:34 +01:00
|
|
|
Short: `Remove the files in path.`,
|
2016-08-05 18:12:27 +02:00
|
|
|
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.
|
|
|
|
|
2020-11-13 11:07:34 +01:00
|
|
|
` + "`rclone delete`" + ` only deletes files but leaves the directory structure
|
2018-11-02 18:07:45 +01:00
|
|
|
alone. If you want to delete a directory and all of its contents use
|
2020-11-13 11:07:34 +01:00
|
|
|
the ` + "`purge`" + ` command.
|
2018-11-02 18:07:45 +01:00
|
|
|
|
2020-04-29 13:15:30 +02:00
|
|
|
If you supply the --rmdirs flag, it will remove all empty directories along with it.
|
2020-11-13 11:07:34 +01:00
|
|
|
You can also use the separate command ` + "`rmdir`" + ` or ` + "`rmdirs`" + ` to
|
|
|
|
delete empty directories only.
|
2020-04-29 13:15:30 +02:00
|
|
|
|
2020-11-13 11:07:34 +01:00
|
|
|
For example, to delete all files bigger than 100MBytes, you may first want to check what
|
|
|
|
would be deleted (use either):
|
2016-08-05 18:12:27 +02:00
|
|
|
|
|
|
|
rclone --min-size 100M lsl remote:path
|
|
|
|
rclone --dry-run --min-size 100M delete remote:path
|
|
|
|
|
2020-11-13 11:07:34 +01:00
|
|
|
Then proceed with the actual delete:
|
2016-08-05 18:12:27 +02:00
|
|
|
|
|
|
|
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
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|