Standard library / os
os.RemoveAll()
Removes a path and descendants, returning the first error if unable to remove everything. Missing paths are not errors. Use only a verified disposable root. Example uses a disposable temporary workspace. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func RemoveAll(path string) errorParameters & Attributes
| Name | Type | Description |
|---|---|---|
| path | string | Pathname interpreted according to this package. |
Return Value
error
Removes a path and descendants, returning the first error if unable to remove everything.
Working Example
Example Snippetgo
package main
import ( "errors" "fmt" "os")
func check(e error) { if e != nil { panic(e) }}func workspace() func() { root, e := os.MkdirTemp("", "bytemind-go-") check(e) old, e := os.Getwd() check(e) check(os.Chdir(root)) return func() { check(os.Chdir(old)); check(os.RemoveAll(root)) }}func main() { defer workspace()() check(os.Mkdir("lab", 0700)) check(os.WriteFile("lab/a", []byte("x"), 0600)) check(os.RemoveAll("lab")) _, e := os.Stat("lab") fmt.Println(errors.Is(e, os.ErrNotExist))}