How to remove all contents of a directory using Golang?

Go

Go Problem Overview


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.

Go Solutions


Solution 1 - Go

Simply do:

os.RemoveAll("/tmp/")
os.MkdirAll("/tmp/",FileMode)

Solution 2 - Go

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)
	}
}

Solution 3 - Go

Just use ioutil.ReadDir to get a slice of os.FileInfo types, then iterate through and remove each child item using os.RemoveAll.

package main

import (
	"io/ioutil"
	"os"
	"path"
)

func main() {
	dir, err := ioutil.ReadDir("/tmp")
	for _, d := range dir {
		os.RemoveAll(path.Join([]string{"tmp", d.Name()}...))
	}
}

That way, you are removing only all the child items and not the parent /tmp folder itself.

I have used this pattern many times before (e.g. test logs, etc.).

Solution 4 - Go

func RemoveContents(dir string) error {
    files, err := filepath.Glob(filepath.Join(dir, "*"))
    if err != nil {
	    return err
	}
	for _, file := range files {
		err = os.RemoveAll(file)
		if err != nil {
			return err
		}
	}
	return nil
}

Solution 5 - Go

You could use filepath.Glob() to get a slice of the contents of the folder, then delete everything it returns. This allows you to use /tmp/* to indicate what you want to delete, and could also be used to delete /tmp/*.go, or /tmp/*/*.go, etc.

package main

import (
	"log"
	"os"
	"path/filepath"
)

func RemoveGlob(path string) (err error) {
	contents, err := filepath.Glob(path)
	if err != nil {
		return
	}
	for _, item := range contents {
		err = os.RemoveAll(item)
		if err != nil {
	 		return
		}
	}
	return
}    

func main() {
	err := RemoveGlob("/tmp/*")
	if err != nil {
		log.Fatalf("Error removing files: %+v", err)
	}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmattlView Question on Stackoverflow
Solution 1 - GoLomithraniView Answer on Stackoverflow
Solution 2 - GopeterSOView Answer on Stackoverflow
Solution 3 - GoopenwonkView Answer on Stackoverflow
Solution 4 - GogaalView Answer on Stackoverflow
Solution 5 - GojsnfwlrView Answer on Stackoverflow