| 1 | package os |
| 2 | |
| 3 | import gos "os" |
| 4 | import "path/filepath" |
| 5 | |
| 6 | // Delete a tree of empty directories. Returns non-nil error if |
| 7 | // an empty directory cannot be deleted. Does not return an error |
| 8 | // if there are non-empty directories. This function can be used |
| 9 | // to prune empty directories from a tree. |
| 10 | func RemoveEmpty(path string) error { |
| 11 | var dirs []string |
| 12 | |
| 13 | return filepath.Walk(path, func(path string, info gos.FileInfo, err error) error { |
| 14 | if info.Mode().IsDir() { |
| 15 | dirs = append(dirs, path) |
| 16 | } |
| 17 | return nil |
| 18 | }) |
| 19 | |
| 20 | for i := len(dirs) - 1; i >= 0; i-- { |
| 21 | err := gos.Remove(dirs[i]) |
| 22 | if err != nil && !IsNotEmpty(err) { |
| 23 | return err |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | return nil |
| 28 | } |