How to Delete Folder with Files from FTP Server by using SSIS Package - SQL Server Integration Services(SSIS) Tutorial


We often have the requirement in which we need to delete the Folder from FTP Server. FTP Task have the operation in which we can Delete the Remote Folder but if FTP Server Folder ( Remote Folder) has the files, FTP Task will fail with below error.
"Error: 0xC001602F at DeleteDirectoryWithFiles, Connection manager "FTPConnection": Deleting folder "/FolderName" ... 550 The directory is not empty. "

How can we delete the folder with Files on FTP Server?



There could be multiple solutions for this requirement. We can use Script task and check if the files exists in Remote Folder or not. If file/s exists then we can use First FTP Task to Delete all the files and second FTP Task to Delete the Folder.

If no file exits then we have to direct our flow to second FTP Task that will delete the remote folder.

Instead of using above solutions, we can simply use the Script Task and handle everything in it. Below code can be used to drop the Directory with or Without Files on FTP Server.

Public Sub Main()

        Dim StrFolderArrary As String()
        Dim StrFileArray As String()
        Dim RemoteDirectory As String
        RemoteDirectory = Dts.Variables("User::RemoteFolder").Value.ToString()

 'FTP connection manager name       
 Dim cm As ConnectionManager = Dts.Connections("FTPConnection")

Dim ftp As FtpClientConnection = 
New FtpClientConnection(cm.AcquireConnection(Nothing))

        ftp.Connect() 'Connecting to FTP Server

        'Provide the Directory that we want to drop


'Get all the files and Folders List        
ftp.GetListing(StrFolderArrary, StrFileArray) 

'If there is no file in the folder, strFile Arry will contain nothing, so close the connection.
        'and delete the directory
        If StrFileArray Is Nothing Then
'If Remote Directory has the files then delete the files first and then delete the directory




        End If

        ' Add your code here
        Dts.TaskResult = ScriptResults.Success
    End Sub

Delete Directory with Files from FTP Server in SSIS Package by using Script Task

Related Posts / Videos on FTP Task / Script Task