W3docs

ftp_delete()

The ftp_delete() function is a PHP built-in function that is used to delete a file on a remote FTP server. The function takes two parameters:

The PHP ftp_delete() Function

ftp_delete() removes a single file from a remote FTP server. It is the FTP equivalent of unlink() on a local filesystem: you pass it an open FTP connection and the path of the file to remove, and it reports back whether the deletion succeeded.

This page covers the function's signature, return value, a complete working example, error handling, and the common pitfalls (deleting directories, relative vs. absolute paths, and permissions).

Syntax

ftp_delete(FTP\Connection $ftp, string $filename): bool
ParameterTypeDescription
$ftpFTP\ConnectionThe connection identifier returned by ftp_connect() (or ftp_ssl_connect()).
$filenamestringThe path of the file to delete on the remote server.

Note: Before PHP 8.1 the first argument was a resource returned by ftp_connect(). From PHP 8.1 it is an FTP\Connection object, but your code does not change — you still just pass the value ftp_connect() returns.

Return value

ftp_delete() returns:

  • true — the file was deleted successfully.
  • false — the deletion failed (file does not exist, you lack permission, the path is a directory, or the connection is invalid).

Because a valid empty path or any failure both surface as false, always test the result explicitly rather than assuming success.

A complete example

You first open a connection with ftp_connect(), authenticate with ftp_login(), delete the file, then close the session with ftp_close():

<?php

// Open an FTP connection (default port 21, 90-second timeout)
$ftp = ftp_connect('ftp.example.com');

if ($ftp === false) {
    exit("Could not connect to the FTP server.\n");
}

// Authenticate
if (!ftp_login($ftp, 'username', 'password')) {
    ftp_close($ftp);
    exit("FTP login failed.\n");
}

// Switch to passive mode — required behind most firewalls/NAT
ftp_pasv($ftp, true);

// Attempt the delete
if (ftp_delete($ftp, '/uploads/old-report.txt')) {
    echo "File deleted successfully.\n";
} else {
    echo "Failed to delete the file.\n";
}

// Always close the connection
ftp_close($ftp);

Handling errors

ftp_delete() emits a PHP warning and returns false when it cannot remove the file. The reliable check is the boolean return value. Use === so the result is not confused with a falsy connection value:

<?php

if (ftp_delete($ftp, '/uploads/old-report.txt') === true) {
    echo "Deleted.\n";
} else {
    echo "Delete failed — check the path, permissions, and that it is a file, not a folder.\n";
}

If you want to suppress the built-in warning and report your own message, prefix the call with @:

<?php

if (@ftp_delete($ftp, $remotePath) === false) {
    error_log("ftp_delete failed for: $remotePath");
}

Common pitfalls

  • Directories are not files. ftp_delete() only removes files. To delete a directory use ftp_rmdir() — and the directory must be empty first, so delete its contents (you can list them with ftp_nlist()) before removing it.
  • Relative paths depend on the current directory. A bare name like report.txt is resolved against the server's current working directory. Prefer absolute paths (/uploads/report.txt), or set the directory explicitly with ftp_chdir().
  • Renaming instead of deleting. If you only need to move or archive a file, use ftp_rename() rather than deleting and re-uploading.
  • Permissions. Deletion fails silently (returns false) when the FTP user lacks write/delete rights on the target directory.

Summary

ftp_delete() deletes a single file on a remote FTP server and returns a boolean indicating success. Open and authenticate the connection first, switch to passive mode for compatibility, check the return value explicitly, and reach for ftp_rmdir() when you need to remove a directory instead. For the full set of FTP operations, see the PHP FTP reference.

Practice

Practice
What is the purpose of the ftp_delete() function in PHP?
What is the purpose of the ftp_delete() function in PHP?
Was this page helpful?