delete.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Package delete provides the delete command.
  2. package delete
  3. import (
  4. "context"
  5. "strings"
  6. "github.com/rclone/rclone/cmd"
  7. "github.com/rclone/rclone/fs/config/flags"
  8. "github.com/rclone/rclone/fs/operations"
  9. "github.com/spf13/cobra"
  10. )
  11. var (
  12. rmdirs = false
  13. )
  14. func init() {
  15. cmd.Root.AddCommand(commandDefinition)
  16. cmdFlags := commandDefinition.Flags()
  17. flags.BoolVarP(cmdFlags, &rmdirs, "rmdirs", "", rmdirs, "rmdirs removes empty directories but leaves root intact", "")
  18. }
  19. var commandDefinition = &cobra.Command{
  20. Use: "delete remote:path",
  21. Short: `Remove the files in path.`,
  22. // Warning! "|" will be replaced by backticks below
  23. Long: strings.ReplaceAll(`
  24. Remove the files in path. Unlike [purge](/commands/rclone_purge/) it
  25. obeys include/exclude filters so can be used to selectively delete files.
  26. |rclone delete| only deletes files but leaves the directory structure
  27. alone. If you want to delete a directory and all of its contents use
  28. the [purge](/commands/rclone_purge/) command.
  29. If you supply the |--rmdirs| flag, it will remove all empty directories along with it.
  30. You can also use the separate command [rmdir](/commands/rclone_rmdir/) or
  31. [rmdirs](/commands/rclone_rmdirs/) to delete empty directories only.
  32. For example, to delete all files bigger than 100 MiB, you may first want to
  33. check what would be deleted (use either):
  34. rclone --min-size 100M lsl remote:path
  35. rclone --dry-run --min-size 100M delete remote:path
  36. Then proceed with the actual delete:
  37. rclone --min-size 100M delete remote:path
  38. That reads "delete everything with a minimum size of 100 MiB", hence
  39. delete all files bigger than 100 MiB.
  40. **Important**: Since this can cause data loss, test first with the
  41. |--dry-run| or the |--interactive|/|-i| flag.
  42. `, "|", "`"),
  43. Annotations: map[string]string{
  44. "versionIntroduced": "v1.27",
  45. "groups": "Important,Filter,Listing",
  46. },
  47. Run: func(command *cobra.Command, args []string) {
  48. cmd.CheckArgs(1, 1, command, args)
  49. fsrc := cmd.NewFsSrc(args)
  50. cmd.Run(true, false, command, func() error {
  51. if err := operations.Delete(context.Background(), fsrc); err != nil {
  52. return err
  53. }
  54. if rmdirs {
  55. fdst := cmd.NewFsDir(args)
  56. return operations.Rmdirs(context.Background(), fdst, "", true)
  57. }
  58. return nil
  59. })
  60. },
  61. }