title: How to delete folder or file in WordPress plugin
body: <p>I am developing a wordpress plugin which require user to remove (or delete) a directory or file.</p>
  
  <h1>After initiating the WP_Filesystem like this</h1>
  
  <pre><code>function filesystem_init($form_url, $method, $context, $fields = null) {
  global $wp_filesystem;
  /* get credentials */
  if (false === ($creds = request_filesystem_credentials($form_url, $method, false, $context, $fields))) {
  return false;
  }
  
  if (!WP_Filesystem($creds)) {
  /* incorrect connection data - ask for credentials again, now with error message */
  request_filesystem_credentials($form_url, $method, true, $context);
  return false;
  }
  return true; //successfully initiated
  }
  </code></pre>
  
  <h1>When the form is posted I have done this to remove a directory with one single file</h1>
  
  <p><code>WP_PLUGIN_DIR.'/myp/mfold/selectedfolder/index.php';</code></p>
  
  <pre><code>function RemoveData($form_url){
  global $wp_filesystem;
  check_admin_referer('ToRemoveData');
  $ToRemove = $_POST['dirToGo']; 
  $form_fields = array('dirToGo');
  $method = ''; 
  $targetFolder= WP_PLUGIN_DIR . '/myp/mfold/'.$ToRemove; //target folder
  $form_url = wp_nonce_url($form_url, 'ToRemoveData'); //page field with nonce value
  if(!filesystem_init($form_url, $method, $targetFolder, $form_fields))
  return false;
  if($wp_filesystem-&gt;is_dir($targetFolder))
  {   
  $wp_filesystem-&gt;rmdir($targetFolder);
  //WP_Filesystem_Base::delete($targetFolder);        
  return " Successful !";
  }
  else
  {
  return new WP_Error('writing_error', 'Cannot Remove');
  }//return error object
  }//close function
  </code></pre>
  
  <p><strong>This function return Success but the folder or the directory is not deleting.</strong></p>
  
  <p>Any idea anyone Thanks </p>