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