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