How to Remove All Directories and Files in Golang?
Last Updated :
23 Mar, 2020
In Go language, you are allowed to remove all the directories and files from a directory or folder with the help of RemoveAll() function. This function removes all the directories and files from the path you will pass to this function. It will remove everything from the specified path, but returns the first error it encounters. If the specified path does not exist, then this method returns nil. If this method throws an error, then it will be of type *PathError. It is defined under the os package so, you have to import os package in your program for accessing RemoveAll() function.
Syntax:
func RemoveAll(path string) error
Example 1:
package main
import (
"log"
"os"
)
func main() {
err := os.RemoveAll( "/Users/anki/Documents/go" )
if err != nil {
log .Fatal(err)
}
}
|
Output:
Before:
After:
Example 2:
package main
import (
"log"
"os"
)
func main() {
err := os.RemoveAll( "/Users/anki/Documents/new_folder/files" )
if err != nil {
log .Fatal(err)
}
}
|
Output:
Before:
After:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...