code: public static boolean deleteDirectory(File dir)
      {
  
          if (dir.canWrite()) {
              File [] subFiles = dir.listFiles();
  
              for(int i=0; i<subFiles.length; i++) {
                  if (subFiles[i].isDirectory())
                      deleteDirectory(subFiles[i]);
                  else
                      subFiles[i].delete();
              }
  
          }
  
          return dir.delete();
  
      }
docs: Deletes a directory and its contents.