Drupal function to delete folder How I created the folder i want to delete:

$form['logo'] = array(
'#title' =&gt; t('Upload logo'),
'#type' =&gt; 'managed_file',
'#description' =&gt; t('Allowed extensions: .gif .png .jpg'),
'#upload_location' =&gt; 'public://myfolder/',
'#upload_validators' =&gt; array(
  'file_validate_extensions' =&gt; array('gif png jpg jpeg'),
  'file_validate_image_resolution' =&gt; array('500x250'),
),);


Now how to delete it? I have tried this 

file_unmanaged_delete_recursive('public://myfolder/'); 

and 

drupal_rmdir('public://myfolder/');


There are files in the folder I'm trying to delete.
file_unmanaged functions are allowed to fail on managed items, so when you create folder using '#type' =&gt; 'managed_file', they are not the way to go.

drupal_rmdir is only a wrapper around PHP rmdir, and it requires folder to be empty.

It means both solutions you have tried was not able to work.



Correct way is to use EntityFieldQuery to load all files in the folder you want to purge, then use file_delete on them, with $force set to TRUE to remove files that are in use (think twice if that's really what you want to do!). Only when the folder is empty, call drupal_rmdir on it.