Drupal function to delete folder
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.