title: Proper way of deleting files in android
body: <p>My code is for deleting all the files having the string "hss" in their names. this works fine for all files in my internal memory, however it does Not delete files which are present in a folder. </p>
  
  <p>How do I make it work?</p>
  
  <pre><code>public class RemoveApkTraces extends Activity {
  // getting SDcard root path
  File dir = new File(Environment.getExternalStorageDirectory()
          .getAbsolutePath());
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.done_ui);
  
      // at the end of oncreate
      new Handler().postDelayed(new Runnable() {
          @Override
          public void run() {
              new ExplorerDBOperation().execute();
  
          }
      }, 300);
  }
  
  class ExplorerDBOperation extends AsyncTask&lt;Void, Boolean, Boolean&gt; {
      ProgressDialog dialog;
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          dialog = ProgressDialog.show(RemoveApkTraces.this, "Please wait",
                  "Processing.........");
      }
  
      @Override
      protected Boolean doInBackground(Void... params) {
          String Patternjpg = "hss";
          File listFile[] = dir.listFiles();
          if (listFile != null) {
              for (int i = 0; i &lt; listFile.length; i++) {
                  if (listFile[i].isDirectory()) {
  
                  } else {
                      if (listFile[i].getName().contains(Patternjpg)) {
                          // Do what ever u want
                          listFile[i].delete();
                      }
                  }
              }
          }
          return true;
      }
  
      @Override
      protected void onPostExecute(Boolean aBoolean) {
          super.onPostExecute(aBoolean);
          if (dialog != null &amp;&amp; dialog.isShowing()) {
              dialog.dismiss();
          }
      }
  }
  </code></pre>