How to delete a directory / folder in android


/**
 * @param directory directory to remove
 * @return <b>true</b> if all files and sub directories in this directorty are deleted successfully <br/>
 * <b>false</b> if fails to delete this directory <br/>
 * <b>Note:</b> This requires permission <b>"android.permission.WRITE_EXTERNAL_STORAGE"</b>
 */
public static boolean removeDirectory(File directory) {

if (directory == null)

return false;
 if (!directory.exists())
 return true;
 if (!directory.isDirectory())
 return false;

String[] list = directory.list();

if (list != null) {
 for (int i = 0; i < list.length; i++) {
 File entry = new File(directory, list[i]);

if (entry.isDirectory())

{
 if (!removeDirectory(entry))
 return false;
 }
 else
 {
 if (!entry.delete())
 return false;
 }
 }
 }

return directory.delete();
 }

Advertisements

Tags: ,

About krishjlk

http://www.facebook.com/krishna.kanth.121

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: