Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 143771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:08:52+00:00 2026-05-11T08:08:52+00:00

Supposing I have a File f that represents a directory, then f.delete() will only

  • 0

Supposing I have a File f that represents a directory, then f.delete() will only delete the directory if it is empty. I’ve found a couple of examples online that use File.listFiles() or File.list() to get all the files in the directory and then recursively traverses the directory structure and delete all the files. However, since it’s possible to create infinitely recursive directory structures (in both Windows and Linux (with symbolic links)) presumably it’s possible that programs written in this style might never terminate.

So, is there a better way to write such a program so that it doesn’t fall into these pitfalls? Do I need to keep track of everywhere I’ve traversed and make sure I don’t go around in circles or is there a nicer way?

Update: In response to some of the answers (thanks guys!) – I’d rather the code didn’t follow symbolic links and stayed within the directory it was supposed to delete. Can I rely on the Commons-IO implementation to do that, even in the Windows case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:08:53+00:00Added an answer on May 11, 2026 at 8:08 am

    If you really want your recursive directory deletion to follow through symbolic links, then I don’t think there is any platform independent way of doing so without keeping track of all the directories you have traversed.

    However, in pretty much every case I can think of you would just want to delete the actual symbolic link pointing to the directory rather than recursively following through the symbolic link.

    If this is the behaviour you want then you can use the FileUtils.deleteDirectory method in Apache Commons IO.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Supposing I have a merge module that installs a file MyFile.txt to a certain
Just a quick check really. I have an XML file that I will be
Supposing I have a source file that looks like this: public class FieldReference {
Supposing that I have a standard EAR file (and the source files that were
I have a file that declares a namespace in the beginning, then does an
MVC newbie question re binders. Supposing I have two strongly typed partial actions that
Supposing that I have millions of user profiles, with hundreds of fields (name, gender,
I have a rather large DBML file and recently discovered that Visual Studio's MSLinqToSQLGenerator
I have a Flash object in a SWF file. It contains an image that
I have a program that's creating a secure directory for user output. This is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.