I am trying to remove a directory with rmdir, but I received the ‘Directory not empty’ message, because it still has files in it.
What function can I use to remove a directory with all the files in it as well?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is no built-in function to do this, but see the comments at the bottom of https://www.php.net/rmdir. A number of commenters posted their own recursive directory deletion functions. You can take your pick from those.
Here’s one that looks decent:
You could just invoke
rm -rfif you want to keep things simple. That does make your script UNIX-only, so beware of that. If you go that route I would try something like: