title: What's the best strategy to delete a very huge folder using Perl?
body: <p>I need to delete all content (files and folders) under a given folder. The problems is the folder has millions of files and folders inside it. So I <strong>don't want to load all the file names in one go</strong>.</p>
  
  <p>Logic should be like this:</p>
  
  <ul>
  <li>iterate a folder without load everything</li>
  <li>get a file or folder</li>
  <li>delete it 
  <strong>(verbose that the file or folder "X" was deleted)</strong></li>
  <li>go to the next one</li>
  </ul>
  
  <p>I'm trying something like this:</p>
  
  <pre><code>sub main(){
    my ($rc, $help, $debug, $root)   = ();
    $rc = GetOptions ( "HELP"           =&gt; \$help,
                       "DEBUG"          =&gt; \$debug,
                       "ROOT=s"         =&gt; \$root);
  
    die "Bad command line options\n$usage\n" unless ($rc);
    if ($help) { print $usage; exit (0); }
  
    if ($debug) {
        warn "\nProceeding to execution with following parameters: \n";
        warn "===============================================================\n";
        warn "ROOT = $root\n";
  
    } # write debug information to STDERR
  
    print "\n Starting to delete...\n";  
  
    die "usage: $0 dir ..\n" unless $root;
    *name = *File::Find::name;
    find \&amp;verbose, @ARGV;
  
  }
  
  sub verbose {
      if (!-l &amp;&amp; -d _) {
          print "rmdir $name\n";
      } else {
          print "unlink $name\n";
      }
  }
  
  main();
  </code></pre>
  
  <p>It's working fine, but whenever "find" reads the huge folder, the application gets stuck and I can see the system memory for Perl increasing until timeout. Why? Is it trying to load all the files in one go? </p>
  
  <p>Thanks for your help.</p>