From 0ac5f90dbb2b2fe79d663b0a3a7f04dca13c196b Mon Sep 17 00:00:00 2001 From: "tanishq.singhal" Date: Sat, 13 May 2023 13:03:00 +0530 Subject: [PATCH] Implemented Delete All Resources Function --- client.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/client.go b/client.go index fb0a110b..69cdd364 100644 --- a/client.go +++ b/client.go @@ -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