Hello everyone! I’m trying to write a plugin that perfectly deletes directories after deleting the resource itself, but at the end I also want to permanently delete the resource without moving it to the trash, how can I do it?
Below is the code that is great for deleting resource directories, but after I select the resource in the resource panel and click “Delete” it turns red and just moves to the trash, and I need it to be permanently deleted immediately…
<?php
/**
* Plugin to delete associated folders of a resource when the resource is deleted.
*/
$deleteFolderFiles = $modx->event->name;
switch($deleteFolderFiles) {
case 'OnDocFormDelete':
// Define the paths of the folders to check and delete
$foldersToDelete = array(
'../assets/AdditionalPhotos/' . $resource->get('id') . '/',
'../upload/' . $resource->get('id') . '/'
);
// Loop through the folders and delete them
foreach ($foldersToDelete as $folder) {
if (is_dir($folder)) {
// Delete all files and subdirectories within the folder
$files = glob($folder . '/*');
foreach ($files as $file) {
if (is_dir($file)) {
// Delete subdirectory and its contents
$subFiles = glob($file . '/*');
foreach ($subFiles as $subFile) {
unlink($subFile);
}
rmdir($file);
} else {
// Delete file
unlink($file);
}
}
// Delete the folder itself
rmdir($folder);
}
}
// Delete resource
$resource->remove($resource->get('id'));
break;
}
This particular line does not work
// Delete resource
$resource->remove($resource->get('id'));