I want to delete selected folder along with all files inside that folder. I tried by using file_unmanaged_delete_recursive()
It will delete folders and files but it is not removing database entry of files and not even it is making the Status of the file to Zero. So how can I achieve this?
Add a comment
|
1 Answer
file_unmanaged_delete_recursive()
, as the name implies, removes any files under a directory without concern to managed files (i.e. files tracked in the database).
In order to remove files from the database, you can make use of the file_delete()
function in the File API. This function needs the file ID supplied as an argument, so you'll need to perform a query on the database to retrieve it. For example:
$files = file_scan_directory('sites/default/files/my_custom_dir', '/.*/', array('recurse' => TRUE));
foreach($files as $file) {
$scanned_file_uri = file_build_uri($file->uri);
// @TODO: query file_managed.uri for match with $scanned_file_uri
// then delete with file_delete();
}