I'm new to Go and can't seem to find a way to delete all the contents of a directory when I don't know the contents.
I've tried:
os.RemoveAll("/tmp/*") os.Remove("/tmp/*")
but get remove *: no such file or directory
or invalid argument
.
And of course if you do:
os.RemoveAll("/tmp/")
it deletes the tmp
directory as well. Which is not what I want.
Readdirnames(n int) function reads the contents of the directory associated with file and returns a slice of up to n names of files in the directory, in directory order. If n > 0, Readdirnames returns at most n names. In this case, if Readdirnames returns an empty slice, it will return a non-nil error explaining why.
Use a directory_iterator and delete the contents yourself (call remove_all on each member of the folder), or delete the whole thing and then recreate the folder.
Delete a single file The Remove function from the os package takes the file path as a parameter and deletes the file. It only deletes a single file. Below is an example of using the remove function. Recommended: Download Files in GoLang. 2. Delete an entire directory The RemoveAll () function completely deletes the path.
A directory is sometimes also called a folder. In Go, we can list directories with ioutil.ReadDir, filepath.Walk, or filepath.Glob . The ioutil.ReadDir reads the directory and returns a list of directory entries sorted by filename. This is the syntax of the ReadDir function.
The package is the os package in the Go standard library. Readdir reads the contents of the directory associated with file and returns a slice of up to n FileInfo values, as would be returned by Lstat, in directory order. Subsequent calls on the same file will yield further FileInfos.
Use the os.Remove method with Readdir to delete all the files in a directory. Os.Remove. A directory may have many files in it. With os.Remove, we can remove just 1 file at a time. But it is possible to use a loop to remove all files in the directory.
It might be a stupid answer, but why not simply do?
os.RemoveAll("/tmp/") os.MkdirAll("/tmp/",FileMode)
Write a simple RemoveContents
function. For example,
package main import ( "fmt" "os" "path/filepath" "strings" ) func RemoveContents(dir string) error { d, err := os.Open(dir) if err != nil { return err } defer d.Close() names, err := d.Readdirnames(-1) if err != nil { return err } for _, name := range names { err = os.RemoveAll(filepath.Join(dir, name)) if err != nil { return err } } return nil } func main() { dir := strings.TrimSuffix(filepath.Base(os.Args[0]), filepath.Ext(os.Args[0])) dir = filepath.Join(os.TempDir(), dir) dirs := filepath.Join(dir, `tmpdir`) err := os.MkdirAll(dirs, 0777) if err != nil { fmt.Println(err) os.Exit(1) } file := filepath.Join(dir, `tmpfile`) f, err := os.Create(file) if err != nil { fmt.Println(err) os.Exit(1) } f.Close() file = filepath.Join(dirs, `tmpfile`) f, err = os.Create(file) if err != nil { fmt.Println(err) os.Exit(1) } f.Close() err = RemoveContents(dir) if err != nil { fmt.Println(err) os.Exit(1) } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With