0

How I created the folder i want to delete:

$form['logo'] = array(
'#title' => t('Upload logo'),
'#type' => 'managed_file',
'#description' => t('Allowed extensions: .gif .png .jpg'),
'#upload_location' => 'public://myfolder/',
'#upload_validators' => array(
  'file_validate_extensions' => array('gif png jpg jpeg'),
  'file_validate_image_resolution' => 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.

0

1 Answer 1

2

file_unmanaged functions are allowed to fail on managed items, so when you create folder using '#type' => '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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.