2022-08-28 11:21:57 +00:00
|
|
|
// Package delete provides the delete command.
|
2016-08-05 16:12:27 +00:00
|
|
|
package delete
|
|
|
|
|
|
|
|
import (
|
2019-06-17 08:34:30 +00:00
|
|
|
"context"
|
2020-05-25 07:50:20 +00:00
|
|
|
"strings"
|
2019-06-17 08:34:30 +00:00
|
|
|
|
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()
|
2023-07-10 17:34:10 +00:00
|
|
|
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.`,
|
2020-05-25 07:50:20 +00:00
|
|
|
// Warning! "|" will be replaced by backticks below
|
2024-08-12 16:17:46 +00:00
|
|
|
Long: strings.ReplaceAll(`Remove the files in path. Unlike [purge](/commands/rclone_purge/) it
|
2022-06-19 13:51:37 +00:00
|
|
|
obeys include/exclude filters so can be used to selectively delete files.
|
2016-08-05 16:12:27 +00:00
|
|
|
|
2020-05-25 07:50:20 +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
|
2022-06-19 13:51:37 +00:00
|
|
|
the [purge](/commands/rclone_purge/) command.
|
2018-11-02 17:07:45 +00:00
|
|
|
|
2020-05-25 07:50:20 +00:00
|
|
|
If you supply the |--rmdirs| flag, it will remove all empty directories along with it.
|
2022-06-19 13:51:37 +00:00
|
|
|
You can also use the separate command [rmdir](/commands/rclone_rmdir/) or
|
|
|
|
[rmdirs](/commands/rclone_rmdirs/) to delete empty directories only.
|
2020-04-29 11:15:30 +00:00
|
|
|
|
2021-03-02 19:11:57 +00:00
|
|
|
For example, to delete all files bigger than 100 MiB, 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
|
|
|
|
|
2021-03-02 19:11:57 +00:00
|
|
|
That reads "delete everything with a minimum size of 100 MiB", hence
|
|
|
|
delete all files bigger than 100 MiB.
|
2020-06-05 16:04:23 +00:00
|
|
|
|
|
|
|
**Important**: Since this can cause data loss, test first with the
|
2020-05-25 07:50:20 +00:00
|
|
|
|--dry-run| or the |--interactive|/|-i| flag.
|
|
|
|
`, "|", "`"),
|
2022-11-26 22:40:49 +00:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"versionIntroduced": "v1.27",
|
2023-07-10 17:34:10 +00:00
|
|
|
"groups": "Important,Filter,Listing",
|
2022-11-26 22:40:49 +00:00
|
|
|
},
|
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
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|