Here you can find the source of deleteDir(File directory)
public static boolean deleteDir(File directory)
//package com.java2s; /*// www . j a va 2 s . co m * Copyright 2008 brunella ltd * * Licensed under the GPL Version 3 (the "License"); * you may not use this file except in compliance with the License. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ import java.io.*; public class Main { public static boolean deleteDir(File directory) { boolean ok = true; File[] children = directory.listFiles(); if (children != null) { for (File file : children) { if (!file.isDirectory()) { ok = ok && file.delete(); } else { ok = ok && deleteDir(file); } } } ok = ok && directory.delete(); return ok; } }