Golang Delete File: How to Remove File in Go

Deleting files in Go is that simple. File delete, file create, file read, and file writes. Almost all the file operations are done through the os package. So if you want to manage files in Golang, you need to use Golang’s built-in os package.

Golang delete file

To delete a file in Golang, use the os.Remove() function. The os.Remove() is a built-in Golang function that removes a file. Provide a filepath to that file, and the function removes that file. It removes the named file or (empty) directory.

If there is an error, that will be of type *PathError. PathError means the program could not find the provided file in a particular path; that is why it throws an error.

See the following syntax.

func Remove(name string) error

It accepts one argument and that file filename(filepath). Next, we specify a target directory. You will want to adjust this to point to a location on your computer. Use a path syntax for your target platform. The Remove() function removes the named file or (empty) directory.

The sample snippet to remove a file in go is the following.

path := "/path/to/file/to/delete"
err := os.Remove(path)

if err != nil {
  fmt.Println(err)
  return
}

Now let’s say I have one file called todo.tmpl, and if I want to remove it, my code will follow.

// hello.go

package main

import (
	"fmt"
	"os"
)

func main() {
	err := os.Remove("todo.tmpl")

	if err != nil {
		fmt.Println(err)
		return
	}
       fmt.Println("File todo.tmpl successfully deleted")
}

We need to pass the filepath in the function parameter and be done. It will remove the file.

Run the program and see the output.

➜  go run hello.go
remove todo.tmpl: no such file or directory

Now, let’s rerun the file, and we will get the error because the file is already removed, and this time, it won’t be there to delete so that it will throw an error.

➜ go run hello.go
remove todo.tmpl: no such file or directory

Before deleting many files, it sometimes helps to have a confirmation or some logic test.

It can be hard to recover from a mass deletion made in error.

We can directly invoke os.Remove() with a path argument to delete a file. And in a loop, we can delete the entire contents of a directory.

func RemoveAll()

Golang RemoveAll() function removes the path and any children it contains.

The RemoveAll() method removes everything it can but returns the first error it encounters. RemoveAll returns nil (no error) if the path does not exist. If there is an error, it will be of type *PathError.

See the following syntax of the function.

func RemoveAll(path string) error

Conclusion

If we want to remove just one file, use Remove(), and if we remove a complete folder and its files, then use RemoveAll() function.

That’s it for this tutorial.

See also

How to create a file in Golang

How to open a file in Golang

How to write a file in Golang

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.