PHP ftp_delete() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_delete() removes a file from the FTP server. It is the remote version of unlink().

You pass the connection and the path of the file. It only works for files. To remove a folder, use ftp_rmdir() instead, and make sure the folder is empty first.

Syntax

Call ftp_delete() with the values you need. Start with this line.

PHP
$ok = ftp_delete($ftp, "old-report.pdf");
CallWhat it does
ftp_delete($ftp, "old-report.pdf")Deletes the file from the current remote folder.
ftp_delete($ftp, "/public_html/tmp/cache.txt")Deletes a file using an absolute path.
@ftp_delete($ftp, "missing.txt")Returns false quietly if the file does not exist.

From PHP 8.1 the first argument is an FTP\Connection object. A failed delete also raises a warning, so use the return value, and optionally @, to handle errors.

Library declarations
PHP
ftp_delete(FTP\Connection $ftp, string $filename): bool

Parameters

ParameterDescription
ftpRequired. The FTP\Connection object returned by ftp_connect() or ftp_ssl_connect().
filenameRequired. The path of the remote file to delete.

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Delete a Remote File

Remove one file and print the result. The host and login are placeholders.

PHP
<?php
$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "demo_user", "demo_pass");

if (ftp_delete($ftp, "old-report.pdf")) {
    echo "File deleted.", PHP_EOL;
} else {
    echo "Could not delete the file.", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. We connect and log in first.
  2. ftp_delete() asks the server to remove old-report. pdf.
  3. A true result means the file is gone.
  4. Otherwise the script prints an error message.

Example 2 — Deleting a File That Does Not Exist

ftp_delete() returns false and raises a warning when the file is missing. Use @ to hide it and handle the result.

PHP
<?php
$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "demo_user", "demo_pass");

$ok = @ftp_delete($ftp, "does-not-exist.txt");

var_dump($ok);

ftp_close($ftp);

How It Works

  1. The file does-not-exist. txt is not on the server.
  2. The server answers with an error such as 550 File not found.
  3. ftp_delete() returns false, and the @ operator hides the PHP warning.
  4. var_dump() prints bool(false) so we can react to it.

Example 3 — Delete Several Files in a Loop

Loop through a list of file names and report which ones were removed.

PHP
<?php
$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "demo_user", "demo_pass");

$files = ["a.tmp", "b.tmp", "c.tmp"];

foreach ($files as $file) {
    $status = @ftp_delete($ftp, $file) ? "deleted" : "failed";
    echo $file, ": ", $status, PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. We keep the file names in an array.
  2. foreach calls ftp_delete() for each name.
  3. The ternary operator turns the true or false result into a word.
  4. In this sample, c. tmp could not be deleted, perhaps because it was missing.

Frequently Asked Questions

It deletes a file on the FTP server. It returns true if the file was removed and false if it failed.
No. Use ftp_rmdir() to remove an empty folder. Delete the files inside it first with ftp_delete().
No. FTP has no recycle bin, so the file is gone for good unless you have a backup.
The file may not exist, the path may be wrong, or your account may not have permission to delete it.
Give the full path, such as ftp_delete($ftp, '/public_html/tmp/a.txt'), or use ftp_chdir() first and then pass just the file name.

Next: ftp_exec()

ftp_exec() asks the FTP server to run a command using the SITE EXEC feature.

ftp_exec() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful