467,858 Members | 1,704 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,858 developers. It's quick & easy.

Recursive delete of directory - Directory not empty error

I'm trying to remove a directory and all its contents from within a
script. I wrote a recursive function to take care of it, but when I
run it I get random "Directory not empty" error messages.

I dropped some code in to echo out the name of the file that's about
to be rmdir()ed or unoink()ed (depending if it's a file or a
directory) to see if it was choking on a particular subdirectory, but
doing that causes all the error messages to stop appearing!

It looks to me as if at some stages of the script the delete commands
are being issued too rapidly and the result is that an attempt to
delete a parent is made before its children have all been deleted.
The code I added to echo out the directory name first probably
introduces just enough of a delay for this problem to go away.

Obviously just doing something in the function to cause a delay isn't
a good solution, is there a better way of handling this problem?

I'm running PHP 5 on a Windows development machine through Apache.
Directory listings are being retrieved from a database.
Jun 2 '08 #1
  • viewed: 3214
Share:
5 Replies
Gordon wrote:
I'm trying to remove a directory and all its contents from within a
script. I wrote a recursive function to take care of it, but when I
run it I get random "Directory not empty" error messages.

I dropped some code in to echo out the name of the file that's about
to be rmdir()ed or unoink()ed (depending if it's a file or a
directory) to see if it was choking on a particular subdirectory, but
doing that causes all the error messages to stop appearing!

It looks to me as if at some stages of the script the delete commands
are being issued too rapidly and the result is that an attempt to
delete a parent is made before its children have all been deleted.
The code I added to echo out the directory name first probably
introduces just enough of a delay for this problem to go away.

Obviously just doing something in the function to cause a delay isn't
a good solution, is there a better way of handling this problem?

I'm running PHP 5 on a Windows development machine through Apache.
Directory listings are being retrieved from a database.
Damn it. I started looking at your problem and my crystal ball went down!

Maybe someone else's crystal ball is still working and they can see you
code.
Jun 2 '08 #2
On Apr 14, 10:04 pm, "Paul Lautman" <paul.laut...@btinternet.com>
wrote:
Gordon wrote:
I'm trying to remove a directory and all its contents from within a
script. I wrote a recursive function to take care of it, but when I
run it I get random "Directory not empty" error messages.
I dropped some code in to echo out the name of the file that's about
to be rmdir()ed or unoink()ed (depending if it's a file or a
directory) to see if it was choking on a particular subdirectory, but
doing that causes all the error messages to stop appearing!
It looks to me as if at some stages of the script the delete commands
are being issued too rapidly and the result is that an attempt to
delete a parent is made before its children have all been deleted.
The code I added to echo out the directory name first probably
introduces just enough of a delay for this problem to go away.
Obviously just doing something in the function to cause a delay isn't
a good solution, is there a better way of handling this problem?
I'm running PHP 5 on a Windows development machine through Apache.
Directory listings are being retrieved from a database.

Damn it. I started looking at your problem and my crystal ball went down!

Maybe someone else's crystal ball is still working and they can see you
code.
I am trying to trim the code down to what's pertinant, but there are a
few classes involved. Please bear with me a moment.
Jun 2 '08 #3
Gordon escribió:
I'm trying to remove a directory and all its contents from within a
script. I wrote a recursive function to take care of it, but when I
run it I get random "Directory not empty" error messages.
In the manual page for unlink() there's a user comment with sample code
for recursive deletion. I haven't tried it but who knows:

http://php.net/unlink


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Jun 2 '08 #4
Álvaro G. Vicario wrote:
Gordon escribió:
>I'm trying to remove a directory and all its contents from within a
script. I wrote a recursive function to take care of it, but when I
run it I get random "Directory not empty" error messages.

In the manual page for unlink() there's a user comment with sample code
for recursive deletion. I haven't tried it but who knows:

http://php.net/unlink

Normally thats because there are hidden or wrong permission files in it.

I suspect you need more subtle code, and if in Linux etc, some form of
attention to permissions if these are the problem.
Jun 2 '08 #5
On Apr 15, 2:28 pm, The Natural Philosopher <a...@b.cwrote:
Álvaro G. Vicario wrote:
Gordon escribió:
I'm trying to remove a directory and all its contents from within a
script. I wrote a recursive function to take care of it, but when I
run it I get random "Directory not empty" error messages.
In the manual page for unlink() there's a user comment with sample code
for recursive deletion. I haven't tried it but who knows:
http://php.net/unlink

Normally thats because there are hidden or wrong permission files in it.

I suspect you need more subtle code, and if in Linux etc, some form of
attention to permissions if these are the problem.
I don't think permissions are the problem, because the development
machine is Windows, and the error message being given is Directory Not
Empty. No Permission Denied errors are popping up in the output. I
also mentioned that the problem went away when I introduced a line of
code for debugging purposes into CmsItem::deleteItem(), which echos
the path being deleted to the output. My suspicion is that the calls
to rmdir are happening more rapidly than the filesystem can cope with
them. The debug code introduced just enough of a delay for the code
to work properly but without it attempts to delete a parent directory
can occur before the deletion of its children has completed, causing
the not empty error.
Jun 2 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by betterdie | last post: by
5 posts views Thread by Jandre | last post: by
24 posts views Thread by biganthony via AccessMonster.com | last post: by
10 posts views Thread by AsheeG87 | last post: by
2 posts views Thread by Martin Marcher | last post: by
3 posts views Thread by from.future.import | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.