Business Directory and Listing Development
In today’s highly competitive digital world, businesses must find innovative ways to gain visibility, attract customers, and boost local reach. One effective and time-tested strategy is developi
Read MoreSign Up Now and Get FREE CTO-level Consultation.
How to delete all files and sub-folders from a folder using php. Like deleting all files of a user without activity for long time, deleting files older than 3 months etc. Whatever the reason, doing such clean-up manually is a tedious task. You can accomplish it with the help of an automated php script. And we are going to explain how to delete directory and all files and sub directories from it with php.
Deleting all files and subfolders from a directory in PHP is a common task for developers managing temporary files, logs, cache directories, or user-uploaded data. Whether you're building a file manager or automating cleanup processes, knowing how to programmatically remove everything inside a folder is essential.
In this post, we’ll explore how to delete all files and subdirectories from a given folder using PHP, step-by-step. We’ll also share a reusable function you can plug into your projects.
glob() and unlink() only two native php functions.
Here is the simple php function I have written to delete all files from a directory. Next we loop through the files one by one and check if the item is a file and not a sub folder itself using is_file() function.
Delete only Particular file type for Eg:pdf, Jpg. with the file extension name user can delete only particular file type from the folder.
Deleteing old and unwanted files by setting CRON job user can delete the files older then 90days.
Always validate folder paths to avoid directory traversal vulnerabilities.
Never allow user inputs directly into folder paths.
Use absolute paths whenever possible.
If you also want to delete the folder itself, simply add the following after the function call:
rmdir($targetFolder);
With this simple function, you can efficiently clear a directory of all files and subfolders in PHP. Always remember to test on non-critical data and validate your paths. Whether you're maintaining a Laravel app, WordPress plugin, or custom CMS, this snippet is a handy tool for your development kit.
Yes, PHP can delete folders and all their contents recursively by looping through directories and files, then calling unlink()
for files and rmdir()
for subdirectories.
The safest way is to use scandir()
to list contents, skip .
and ..
, and apply unlink()
and rmdir()
carefully with absolute paths and permission checks to avoid deleting unintended data.
Use a recursive function to delete all files and subdirectories inside the folder, but do not call rmdir()
on the root folder itself.
Yes, you can check if an item is a file using is_file()
or !is_dir()
and use unlink()
to delete it, skipping directories.
You can use rmdir()
on a directory after ensuring it is empty. To clear a directory, recursively delete its contents before calling rmdir()
.
You can use filemtime()
to check the last modified time of each file and delete it if it’s older than a specified number of seconds.
php
if (time() - filemtime($file) > (7 * 24 * 60 * 60)) { unlink($file); }
PHP requires write and delete permissions on the target directory and its contents to successfully remove files and folders.
unlink()
to delete folders in PHP?No, itunlink()It only
works for files. To delete folders, use rmdir()
after clearing their contents.
It’s not recommended unless paths are strictly validated and sanitized to prevent directory traversal attacks and accidental data loss.
Yes, but recursion is cleaner and handles deep folder structures well. Non-recursive methods require manual directory walking using stacks.
Request a FREE Business Plan.
In today’s highly competitive digital world, businesses must find innovative ways to gain visibility, attract customers, and boost local reach. One effective and time-tested strategy is developi
Read MoreIcons are a crucial part of a well-designed e-commerce website. They improve usability, enhance visual appeal, and make navigation intuitive. However, encountering missing icons in your OpenCart store
Read MoreIn this blog post, we are going to explain how to upload images in WordPress media. The steps to inserting an image into a blog post in WordPress are: Place the cursor where you want the image t
Read More