Recursive delete of directory - Directory not empty error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gordon

    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.
  • Paul Lautman

    #2
    Re: Recursive delete of directory - Directory not empty error

    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.


    Comment

    • Gordon

      #3
      Re: Recursive delete of directory - Directory not empty error

      On Apr 14, 10:04 pm, "Paul Lautman" <paul.laut...@b tinternet.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.

      Comment

      • =?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=

        #4
        Re: Recursive delete of directory - Directory not empty error

        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://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
        --

        Comment

        • The Natural Philosopher

          #5
          Re: Recursive delete of directory - Directory not empty error

          Á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:
          >

          >
          >
          >
          >
          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.

          Comment

          • Gordon

            #6
            Re: Recursive delete of directory - Directory not empty error

            On Apr 15, 2:28 pm, The Natural Philosopher <a...@b.cwrot e:
            Á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:
            >>
            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::delete Item(), 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.

            Comment

            Working...