How to recursively delete a folder with the files within using FtpWebRequest?

Prince OfThief picture Prince OfThief · Feb 1, 2011 · Viewed 15k times · Source

I want to delete a folder in FTP and it's files recursively.

Any example code do I can implement?

Answer

Julien Rubiano picture Julien Rubiano · Jan 7, 2014

First you have to list all your files in your directory :

public static List<string> DirectoryListing(string Path, string ServerAdress, string Login, string Password)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + ServerAdress + Path);
        request.Credentials = new NetworkCredential(Login, Password);

        request.Method = WebRequestMethods.Ftp.ListDirectory;            

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);

        List<string> result = new List<string>();

        while (!reader.EndOfStream)
        {
            result.Add(reader.ReadLine());
        }

        reader.Close();
        response.Close();

        return result;
    }

Then you need a method to delete a single file (because you can delete a folder only if it's empty) :

public static void DeleteFTPFile(string Path, string ServerAdress, string Login, string Password)
    {
        FtpWebRequest clsRequest = (System.Net.FtpWebRequest)WebRequest.Create("ftp://" + ServerAdress + Path);
        clsRequest.Credentials = new System.Net.NetworkCredential(Login, Password);

        clsRequest.Method = WebRequestMethods.Ftp.DeleteFile;

        string result = string.Empty;
        FtpWebResponse response = (FtpWebResponse)clsRequest.GetResponse();
        long size = response.ContentLength;
        Stream datastream = response.GetResponseStream();
        StreamReader sr = new StreamReader(datastream);
        result = sr.ReadToEnd();
        sr.Close();
        datastream.Close();
        response.Close();
    }

And finally :

public static void DeleteFTPDirectory(string Path, string ServerAdress, string Login, string Password)
{
        FtpWebRequest clsRequest = (System.Net.FtpWebRequest)WebRequest.Create("ftp://" + ServerAdress + Path);
        clsRequest.Credentials = new System.Net.NetworkCredential(Login, Password);

        List<string> filesList = DirectoryListing(Path, ServerAdress, Login, Password);

        foreach (string file in filesList)
        {
            DeleteFTPFile(Path + file, ServerAdress, Login, Password);
        }

        clsRequest.Method = WebRequestMethods.Ftp.RemoveDirectory;

        string result = string.Empty;
        FtpWebResponse response = (FtpWebResponse)clsRequest.GetResponse();
        long size = response.ContentLength;
        Stream datastream = response.GetResponseStream();
        StreamReader sr = new StreamReader(datastream);
        result = sr.ReadToEnd();
        sr.Close();
        datastream.Close();
        response.Close();
    } 

You can easily call this like that (for me those methods are in a class called "Ftp") :

Ftp.DeleteFTPDirectory(the_path_of_your_folder_in_ftp,your_server_address,your_ftp_login,your_ftp_password);

Of course, you'll need to customize those lines, but it worked perfectly for me :)