Recursive Delete with FTP

I was recently creating an administration application for a client. The app involved working with files and directories as well as uploading and deleting files or directories. While I was developing, I ran into an issue that required a special function: recursive delete.

A recursive function is a function that has the ability to call itself (recursion). I ran into this problem while trying to delete a directory containing files and/or other directories. I was using FTP at the time so, the function will be written as such. It can be easily ported to using filesystem functions by following the same logic/flow.

Anyway, the use of ftp_rmdir() was not enough. Using it to delete a directory containing files or folders only throws an error stating that the folder you are trying to delete contains other files. It was a little disappointing to see that PHP does not have a recursive delete function built in. So, I decided to write my own function. It works very well, it is small and it runs fast!

Well, here is the code:

# server credentials
$host = "ftp server";
$user = "username";
$pass = "password";

# connect to ftp server
$handle = @ftp_connect( $host ) or die( "Could not connect to $host" );

# login using credentials
@ftp_login( $handle, $user, $pass ) or die( "Could not login to $host" );

function recursiveDelete( $directory ) {
    # here we attempt to delete the file/directory
    if( !( @ftp_rmdir( $handle, $directory ) || @ftp_delete( $handle, $directory ) ) ) {
        # if the attempt to delete fails, get the file listing
        $filelist = @ftp_nlist( $handle, $directory );

        # loop through the file list and recursively delete the FILE in the list
        foreach ( $filelist as $file ) {
            recursiveDelete( $file );

        #if the file list is empty, delete the DIRECTORY we passed
        recursiveDelete( $directory );

The comments explain what is going on but, I will explain a little more right now. Basically, you pass the directory you want to delete to the function. The function will try to delete the file/directory that was passed to it. If the function fails to delete the file/directory, it will attempt to enter the directory and get a file list. The function then loops through the file list and calls itself by passing the file or directory from the list. It will repeat this until there are no more files or directories inside the directory you passed for removal. Once that is complete, it will re-attempt to delete the directory you passed to it.

Thats it! Very small script, yet very powerful. Be careful when creating these kinds of functions as you don’t want the function to accidentally delete more than you intended. Just a quick note about the function, make sure you include a trailing slash at the end of the directory! If you forget to, this function will try and delete the files and directories where the directory you passed is contained. It’s a good habit to get into anyway. I was flooding the FTP server at work with requests to delete directories I didn’t want to delete because I forgot to add the trailing slash! Good thing the file permissions stopped it!