6 Ways To Delete Files In PHP (Simple Examples)

Welcome to a tutorial on how to delete files in PHP. Need to remove some old, backup, or temporary files in PHP?

  • To delete a file in PHP, use unlink("TARGET FILE");
  • To remove an empty folder, use rmdir("FOLDER");

That covers the super quick basics, but “advanced deleting” entire folders and only certain file types is a different story. Just how do we do those? Read on for more examples!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

WAYS TO DELETE FILES IN PHP

All right, let us now get into the examples of how to delete files in PHP.

 

1) SIMPLE DELETE

1-basic.php
<?php
// (A) DELETE FILE
echo unlink("file.txt") ? "OK" : "ERROR" ;
 
// (B) DELETE FOLDER
echo rmdir("d:/test") ? "OK" : "ERROR" ;

As in the introduction above, we use unlink() to delete files and rmdir() to remove directories. Take note that both functions will return true/false to indicate if the process is successful.

P.S. Copy-protected files cannot be deleted, and folders that are not empty cannot be removed.

 

2) DELETE IF FILE EXISTS

2-delete-exist.php
<?php
// (A) FILE TO DELETE
$file = "file.txt";
 
// (B) CHECK BEFORE DELETE
if (file_exists($file)) { echo unlink($file) ? "OK" : "ERROR" ; }
else { echo "$file NOT FOUND"; }

PHP will throw warnings when deleting files that do not exist. So here is a “quick fix” – Just do a file_exists() check before proceeding with the delete.

 

 

3) DELETE ONLY CERTAIN FILE TYPES

3-delete-type.php
<?php
// (A) GET ALL FILES OF SPECIFIED EXTENSION
$folder = "d:/test/";
$ext = "jpg,png,gif";
$files = glob("$folder*.{$ext}", GLOB_BRACE);
 
// (B) LOOP AND DELETE
if (count($files)>0) {
  foreach ($files as $f) { unlink($f); }
}

Only want to clear out certain file types in a folder? Use the glob() function to help filter out a list of the selected files, then delete them one by one.

 

4) DELETE FILES ONLY IF OLDER THAN A SET DATE

4-delete-old.php
<?php
// (A) FOLDER & KEEP DATE
$folder = "d:/test/";
$keep = strtotime("2022-12-01");
$files = glob("$folder*", GLOB_BRACE);

// (B) DELETE ONLY IF OLDER THAN SET DATE
if (count($files)>0) { foreach ($files as $f) {
  if (filemtime($f) < $keep) { unlink($f); }
}}

Want to remove some old backups or outdated files? It’s pretty much the same story of using good old glob() to get the list of files in the folder. Then using filemtime() to check against the “file last modified” time – Delete the file if it is older than a set date.

 

 

5) DELETE THE ENTIRE FOLDER

5-delete-folder.php
<?php
function delFolder ($folder) {
  // (A1) GET ALL FILES + FOLDERS
  $all = glob("$folder*", GLOB_BRACE);

  // (A2) DELETE FILES & RECURSIVE DELETE SUB-FOLDERS
  if (count($all)>0) { foreach ($all as $a) {
    if (is_dir($a)) { delFolder("$a/"); }
    else { echo unlink($a); }
  }}
 
  // (A3) DELETE CURRENT FOLDER ITSELF
  rmdir($folder);
}

// (B) GO!
delFolder("d:/test/");

Since we can’t use rmdir() to delete an entire folder directly, we have to create a recursive function that will dig into the sub-folders and delete everything inside.

 

6) COMMAND LINE DELETE

6-delete-command.php
<?php
// (A) WINDOWS
echo exec('del "d:\\test\\test.txt"');
 
// (B) LINUX
echo exec('rm /path/file/test.txt');

Finally, this is not quite “delete in PHP”… But we can actually run shell commands in PHP to delete files and folders.

 

 

EXTRAS

That’s all for the main tutorial, and here is a small section on some extras and links that may be useful to you.

 

THE SUMMARY

Function Description Reference Link
unlink(TARGET) Delete target file. Click Here
rmdir(FOLDER) Remove a folder. Click Here
file_exists(FILE) Checks if the target file exists. Click Here
glob(PATTERN) Get all pathnames for the given pattern/target folder. Click Here
exec() Run a shell command. Click Here

 

MORE LINKS

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *