Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all contents of a directory using Golang?

Tags:

go

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.

like image 692
mattl Avatar asked Oct 31 '15 11:10

mattl


People also ask

How do you check if a directory is empty in Golang?

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.

How can I delete all files in a directory in C ++?

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.

How do I delete a single file in Golang?

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.

How do I List A directory in Golang?

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.

What is readdir package in Golang?

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.

How do I delete all files in a directory in Python?

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.


2 Answers

It might be a stupid answer, but why not simply do?

os.RemoveAll("/tmp/") os.MkdirAll("/tmp/",FileMode) 
like image 69
Lomithrani Avatar answered Sep 23 '22 14:09

Lomithrani


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)     } } 
like image 20
peterSO Avatar answered Sep 23 '22 14:09

peterSO