Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Directories / Folders

How do I delete all files and sub folders in a folder? by KevinADC
Posted: 12 Feb 07 (Edited 15 Feb 07)

ignore this section:
delete files
deleting files


Problem :

You want to delete all the files and sub folders (and all their files and sub folders) in a folder. But to delete files you need to use the unlink() function and for folders the rmdir() function, and besides, you don't know how to recursively search through all the sub folders to delete all the files first so you can delete all the folders.

Solution :

Use the File::Path (core) module.

Discussion :

The module is so easy to use that there is really not much to discuss. Click on the link above and read the documentation. You can also use the module to create directory trees.

Important Note

The only thing to keep in mind is that deleting files and folders is most likely going to be a permanent situation. So make sure you really want to delete an  entire directory tree and make sure  you are starting in the correct folder before invoking the script. If you make a mistake and start in the wrong directory you could suffer permanent loss of all the data.

Back to Perl FAQ Index
Back to Perl Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close