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

  • Home
  • SEARCH
  • 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 1101701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:04:23+00:00 2026-05-17T01:04:23+00:00

In Java, I want to delete all the contents that are present in a

  • 0

In Java, I want to delete all the contents that are present in a folder which includes files and folders.

public void startDeleting(String path) {
        List<String> filesList = new ArrayList<String>();
        List<String> folderList = new ArrayList<String>();
        fetchCompleteList(filesList, folderList, path);
        for(String filePath : filesList) {
            File tempFile = new File(filePath);
            tempFile.delete();
        }
        for(String filePath : folderList) {
            File tempFile = new File(filePath);
            tempFile.delete();
        }
    }

private void fetchCompleteList(List<String> filesList, 
    List<String> folderList, String path) {
    File file = new File(path);
    File[] listOfFile = file.listFiles();
    for(File tempFile : listOfFile) {
        if(tempFile.isDirectory()) {
            folderList.add(tempFile.getAbsolutePath());
            fetchCompleteList(filesList, 
                folderList, tempFile.getAbsolutePath());
        } else {
            filesList.add(tempFile.getAbsolutePath());
        }

    }

}

This code does not work, what is the best way to do this?

  • 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. Editorial Team
    Editorial Team
    2026-05-17T01:04:23+00:00Added an answer on May 17, 2026 at 1:04 am

    If you use Apache Commons IO it’s a one-liner:

    FileUtils.deleteDirectory(dir);
    

    See FileUtils.deleteDirectory()



    Guava used to support similar functionality:

    Files.deleteRecursively(dir);
    

    This has been removed from Guava several releases ago.


    While the above version is very simple, it is also pretty dangerous, as it makes a lot of assumptions without telling you. So while it may be safe in most cases, I prefer the “official way” to do it (since Java 7):

    public static void deleteFileOrFolder(final Path path) throws IOException {
      Files.walkFileTree(path, new SimpleFileVisitor<Path>(){
        @Override public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs)
          throws IOException {
          Files.delete(file);
          return CONTINUE;
        }
    
        @Override public FileVisitResult visitFileFailed(final Path file, final IOException e) {
          return handleException(e);
        }
    
        private FileVisitResult handleException(final IOException e) {
          e.printStackTrace(); // replace with more robust error handling
          return TERMINATE;
        }
    
        @Override public FileVisitResult postVisitDirectory(final Path dir, final IOException e)
          throws IOException {
          if(e!=null)return handleException(e);
          Files.delete(dir);
          return CONTINUE;
        }
      });
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

HI I want to write a java program by which I can delete all
I want to execute ANT delete target, so that I can delete all files
I want to create a sample RESTful web service in java which involves all
I want to delete all directories and subdirectories under a root directory that are
I want to create a library(Jar file) in Java which would contain all my
I want to delete a document in lucene 2.4 with java. My code is
I have a Hashtable in Java and want to iterate over all the values
Im a getting errors from some code developed in Java that want to use
I am developing a Java app that reads all the emails from GMAIL and
I want to remove all rows that i entered from my SQLite database table.

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.