delete_resource()

Parameters

ColumnTypeDefaultDescription
$ref

Location

include/resource_functions.php lines 2804 to 2909

Definition

 
function delete_resource($ref)
{
    global 
$userref;
    
# Delete the resource, all related entries in tables and all files on disk
    
$resource get_resource_data($ref);

    if (
        !
$resource
        
||
            (
                (
                
checkperm("D")
                ||
                !
get_edit_access($ref$resource["archive"], $resource)
                ||
                (isset(
$userref) && $resource["lock_user"] > && $resource["lock_user"] != $userref)
                )
            &&
                !
hook('check_single_delete')
            &&
                
PHP_SAPI != 'cli'
            
)
    ) {
        return 
false;
    }

    
$current_state $resource['archive'];

    global 
$resource_deletion_state$staticsync_allow_syncdir_deletion$storagedir;
    if (isset(
$resource_deletion_state) && $current_state != $resource_deletion_state) { # Really delete if already in the 'deleted' state.
        # $resource_deletion_state is set. Do not delete this resource, instead move it to the specified state.
        
update_archive_status($ref$resource_deletion_state$current_state);

        
# log this so that administrator can tell who requested deletion
        
resource_log($refLOG_CODE_DELETED'');

        return 
true;
    }

    
# FStemplate support - do not allow samples from the template to be deleted
    
if (resource_file_readonly($ref)) {
        return 
false;
    }

    
# Is transcoding
    
if ($resource['is_transcoding'] == 1) {
        return 
false;
    } 
# Can't delete when transcoding

    # Delete files first
    
$extensions = array();
    
$extensions[] = $resource['file_extension'] ? $resource['file_extension'] : "jpg";
    
$extensions[] = $resource['preview_extension'] ? $resource['preview_extension'] : "jpg";
    
$extensions[] = $GLOBALS['ffmpeg_preview_extension'];
    
$extensions[] = 'icc'// also remove any extracted icc profiles
    
$extensions array_unique($extensions);

    foreach (
$extensions as $extension) {
        
$sizes get_image_sizes($reftrue$extension);
        foreach (
$sizes as $size) {
            if (
file_exists($size['path']) && ($staticsync_allow_syncdir_deletion || false !== strpos($size['path'], $storagedir))) { // Only delete if file is in filestore
                
$pathtofile realpath($size['path']); // avoid passing relative path to unlink function to prevent error on removal of file.
                
unlink($pathtofile);
            }
        }
    }

    
hook('delete_resource_extra''', array($resource));

    
# Delete any alternative files
    
$alternatives get_alternative_files($ref);
    for (
$n 0$n count($alternatives); $n++) {
        
delete_alternative_file($ref$alternatives[$n]['ref']);
    }

    
// Attempt to remove directory
    
$resource_path get_resource_path($reftrue"pre"true);

    
$dirpath dirname($resource_path);
    
hook('delete_resource_path_extra''', array($dirpath));
    
rcRmdir($dirpath); // try to delete directory, but if we do not have permission fail silently for now

    
hook("beforedeleteresourcefromdb""", array($ref));

    
# Delete all database entries
    
clear_resource_data($ref);
    
resource_log($refLOG_CODE_DELETED_PERMANENTLY'');
    
ps_query("delete from resource where ref=?", array("i",$ref));
    
ps_query("delete from collection_resource where resource=?", array("i",$ref));
    
ps_query("delete from resource_custom_access where resource=?", array("i",$ref));
    
ps_query("delete from external_access_keys where resource=?", array("i",$ref));
    
ps_query("delete from resource_alt_files where resource=?", array("i",$ref));
    
ps_query(
        
"    DELETE an
               FROM annotation_node AS an
         INNER JOIN annotation AS a ON a.ref = an.annotation
              WHERE a.resource = ?"
,
        array(
"i",$ref)
    );
    
ps_query("DELETE FROM annotation WHERE resource = ?", array("i",$ref));
    
hook("afterdeleteresource");

    
clear_query_cache("stats");

    return 
true;
}

This article was last updated 6th April 2025 10:05 Europe/London time based on the source file dated 26th March 2025 19:05 Europe/London time.