2022-08-28 19:21:57 +08:00
|
|
|
// Package rmdir provides the rmdir command.
|
2016-11-28 02:36:13 +08:00
|
|
|
package rmdir
|
|
|
|
|
|
|
|
import (
|
2019-06-17 16:34:30 +08:00
|
|
|
"context"
|
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/fs/operations"
|
2016-11-28 02:36:13 +08:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2017-12-13 18:23:54 +08:00
|
|
|
var (
|
|
|
|
leaveRoot = false
|
|
|
|
)
|
|
|
|
|
2016-11-28 02:36:13 +08:00
|
|
|
func init() {
|
|
|
|
cmd.Root.AddCommand(rmdirsCmd)
|
2017-12-13 18:23:54 +08:00
|
|
|
rmdirsCmd.Flags().BoolVarP(&leaveRoot, "leave-root", "", leaveRoot, "Do not remove root directory if empty")
|
2016-11-28 02:36:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var rmdirsCmd = &cobra.Command{
|
|
|
|
Use: "rmdirs remote:path",
|
2017-07-22 01:17:57 +08:00
|
|
|
Short: `Remove empty directories under the path.`,
|
2024-08-13 00:17:46 +08:00
|
|
|
Long: `This recursively removes any empty directories (including directories
|
2020-11-13 18:07:34 +08:00
|
|
|
that only contain empty directories), that it finds under the path.
|
|
|
|
The root path itself will also be removed if it is empty, unless
|
|
|
|
you supply the ` + "`--leave-root`" + ` flag.
|
2016-11-28 02:36:13 +08:00
|
|
|
|
2022-06-19 21:51:37 +08:00
|
|
|
Use command [rmdir](/commands/rclone_rmdir/) to delete just the empty
|
|
|
|
directory given by path, not recurse.
|
2017-12-13 18:23:54 +08:00
|
|
|
|
2016-11-28 02:36:13 +08:00
|
|
|
This is useful for tidying up remotes that rclone has left a lot of
|
2022-06-19 21:51:37 +08:00
|
|
|
empty directories in. For example the [delete](/commands/rclone_delete/)
|
|
|
|
command will delete files but leave the directory structure (unless
|
|
|
|
used with option ` + "`--rmdirs`" + `).
|
2016-11-28 02:36:13 +08:00
|
|
|
|
2023-08-17 18:05:12 +08:00
|
|
|
This will delete ` + "`--checkers`" + ` directories concurrently so
|
|
|
|
if you have thousands of empty directories consider increasing this number.
|
|
|
|
|
|
|
|
To delete a path and any objects in it, use the [purge](/commands/rclone_purge/)
|
2022-06-19 21:51:37 +08:00
|
|
|
command.
|
2016-11-28 02:36:13 +08:00
|
|
|
`,
|
2022-11-27 06:40:49 +08:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"versionIntroduced": "v1.35",
|
2023-07-11 01:34:10 +08:00
|
|
|
"groups": "Important",
|
2022-11-27 06:40:49 +08:00
|
|
|
},
|
2016-11-28 02:36:13 +08:00
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
2018-05-08 00:58:16 +08:00
|
|
|
fdst := cmd.NewFsDir(args)
|
2016-12-05 00:52:24 +08:00
|
|
|
cmd.Run(true, false, command, func() error {
|
2019-06-17 16:34:30 +08:00
|
|
|
return operations.Rmdirs(context.Background(), fdst, "", leaveRoot)
|
2016-11-28 02:36:13 +08:00
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|