How to delete files from public folder? I am storing some files in  public folder, programmatically I want to delete those files through the provided path. I tried using below code, but not worked.

function delete_public_files($arg1, $arg2){ 
  $uri = 'public://'; 
  $path= file_create_url($uri);
  $fullpath = $path.$arg1.'/'.$arg2;

  //file_unmanaged_delete($fullpath);

  $fid = db_query("SELECT fid FROM {file_managed} WHERE uri = :path", array(':path' =&gt; $fullpath))-&gt;fetchField();
  if ($fid) {
    $file = file_load($fid);
    file_delete($file);
  }
  else {
    file_unmanaged_delete($fullpath);
  }
} 


These files are not recorded in the file_managed table and file_usage table also. So how can I delete these files. If I pass the path to file_delete($fullpath); It will says error that first argument should be object. So how can I achieve all these things. Thanks for any appreciation.
file_delete deletes a file where it is located and its database record also.

First check file_load whether it is rendering an object file.

// load the file
$file = file_load('YOUR_FILE_ID');

// delete file from disk and from database
file_delete($file);


The file_delete() also takes a second argument $force that if set to TRUE will remove the file even if it's reported as in use by the file_usage table.

use drupal_unlink($fullpath) if files are not recorded in the file_managed table and file_usage table.

See this