Skip to content

Commit

Permalink
Implemented Delete All Resources Function
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishq.singhal committed May 13, 2023
1 parent 34d66dd commit 0ac5f90
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,55 @@ func (c *Client) MkdirAll(path string) error {
return nil
}

// DeleteAllResources delete files recursively in the directory and Recursively delete subdirectories.
// An error will be returned if no file or directory with the specified path exists
func (c *Client) DeleteAllResources(filePath string) error {

// Get the file/directory information
fileInfo, err := c.Stat(filePath)
if err != nil {
return err
}

if fileInfo.IsDir() {
// Delete files recursively in the directory
files, err := c.ReadDir(filePath)
if err != nil {
return err
}

for _, file := range files {
if file.IsDir() {
// Recursively delete subdirectories
err = c.DeleteAllResources(filePath + "/" + file.Name())
if err != nil {
return err
}
} else {
// Delete individual files
err = c.Remove(filePath + "/" + file.Name())
if err != nil {
return err
}
}
}

// Delete the empty directory
err = c.RemoveDirectory(filePath)
if err != nil {
return err
}
} else {
// Delete individual files
err = c.Remove(filePath)
if err != nil {
return err
}
}

return nil
}

// File represents a remote file.
type File struct {
c *Client
Expand Down

0 comments on commit 0ac5f90

Please sign in to comment.