OnResourceDelete can't delete folders after deleting a resource

Hello everyone. I’m trying to delete folders along with a resource that I create automatically beforehand. I wrote the following plugin:

$modx->lexicon->load('deletefolderplugin:default');

switch ($modx->event->name) {
    case 'OnResourceDelete':

        // Get the resource ID being deleted
        $resourceId = $modx->event->params['id'];

        // Set the path of the folders to be deleted
        $uploadFolderPath = $modx->getOption('base_path') . 'upload/' . $resourceId . '/';
        $additionalPhotosFolderPath = $modx->getOption('base_path') . 'assets/AdditionalPhotos/' . $resourceId . '/';

        // Remove the folders and their contents
        $modx->log(modX::LOG_LEVEL_INFO, '[DeleteFolderPlugin] Deleting folders ' . $uploadFolderPath . ' and ' . $additionalPhotosFolderPath);
        $modx->cacheManager->deleteTree($uploadFolderPath);
        $modx->cacheManager->deleteTree($additionalPhotosFolderPath);

        break;
}

I put it on the event OnResourceDelete.
But it doesn’t remove folders from the server(

The right on my folders: 755

do you see the log info in the logs?

This is the only thing in the logs.

[2023-03-14 11:44:31] (INFO @ /home/s97117/public_html/test.site.lv/core/vendor/xpdo/xpdo/src/xPDO/Om/xPDOObject.php : 1719) Removed MODX\Revolution\modTemplateVarResource instance with primary key 2674
[2023-03-14 11:44:31] (INFO @ /home/s97117/public_html/test.site.lv/core/vendor/xpdo/xpdo/src/xPDO/Om/xPDOObject.php : 1719) Removed MODX\Revolution\modTemplateVarResource instance with primary key 2675
[2023-03-14 11:44:31] (INFO @ /home/s97117/public_html/test.site.lv/core/vendor/xpdo/xpdo/src/xPDO/Om/xPDOObject.php : 1719) Removed MODX\Revolution\modTemplateVarResource instance with primary key 2676
[2023-03-14 11:44:31] (INFO @ /home/s97117/public_html/test.site.lv/core/vendor/xpdo/xpdo/src/xPDO/Om/xPDOObject.php : 1719) Removed MODX\Revolution\modTemplateVarResource instance with primary key 2674
[2023-03-14 11:44:31] (INFO @ /home/s97117/public_html/test.site.lv/core/vendor/xpdo/xpdo/src/xPDO/Om/xPDOObject.php : 1719) Removed MODX\Revolution\modTemplateVarResource instance with primary key 2675
[2023-03-14 11:44:31] (INFO @ /home/s97117/public_html/test.site.lv/core/vendor/xpdo/xpdo/src/xPDO/Om/xPDOObject.php : 1719) Removed MODX\Revolution\modTemplateVarResource instance with primary key 2676
[2023-03-14 11:44:31] (INFO @ /home/s97117/public_html/test.site.lv/core/vendor/xpdo/xpdo/src/xPDO/Om/xPDOObject.php : 1719) Removed MODX\Revolution\modDocument instance with primary key 457
[2023-03-14 11:44:32] (WARN @ /home/s97117/public_html/test.site.lv/core/src/Revolution/modCacheManager.php : 554) Error caching script elements/modx/revolution/modplugin/13
[2023-03-14 11:44:32] (WARN @ /home/s97117/public_html/test.site.lv/core/src/Revolution/modCacheManager.php : 413) Error caching lexicon topic lexicon/en/core/default
[2023-03-14 11:44:32] (WARN @ /home/s97117/public_html/test.site.lv/core/src/Revolution/modCacheManager.php : 413) Error caching lexicon topic lexicon/en/core/default
[2023-03-14 11:44:32] (WARN @ /home/s97117/public_html/test.site.lv/core/src/Revolution/modCacheManager.php : 413) Error caching lexicon topic lexicon/uk/core/default
[2023-03-14 11:44:32] (WARN @ /home/s97117/public_html/test.site.lv/core/src/Revolution/modCacheManager.php : 413) Error caching lexicon topic lexicon/uk/core/resource
[2023-03-14 11:44:32] (ERROR @ /home/s97117/public_html/test.site.lv/core/vendor/xpdo/xpdo/src/xPDO/Om/xPDOObject.php : 227) Error 42000 executing statement: 
Array
(
    [0] => 42000
    [1] => 1064
    [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0 ORDER BY `modResource`.`pagetitle` ASC LIMIT 20' at line 1
)

So when I try to delete a resource, now for some reason it doesn’t want to turn red and crossed out right away, I need to reload the page so that it is in the “deleted” status and then go to the trash and delete it permanently…

I think I wrote this plugin incorrectly to delete the file folder with the resource(

I think you have to supply the second parameter when you want to use the deleteTree function successfully.
Something like:

$options = ['deleteTop' => true, 'extensions' => ['.jpg']];
$modx->cacheManager->deleteTree($additionalPhotosFolderPath, $options);

Specifying the .jpg extension is not entirely correct, since directories can have any extension))

I just need to delete folders with all files (recursively) that match the id of the deleted resource

Maybe you can supply an empty value for “extensions”.
The problem is, when you don’t supply any options, this function only deletes files with the extension '.cache.php'.

I tried to do this for the OnDocFormDelete action and add a delay, but it still doesn’t work for me, the file folders are not deleted((

<?php
/**
 * Plugin to delete associated folders of a resource when the resource is deleted.
 */

$deleteFolderFiles = $modx->event->name;
switch($deleteFolderFiles) {
    case 'OnDocFormDelete':
        
        
    // Get the ID of the deleted resource
    $resourceId = $resource->get('id');

    // Define the paths of the folders to delete
    $assetsPath = $modx->getOption('base_path') . 'assets/AdditionalPhotos/' . $resourceId . '/';
    $uploadPath = $modx->getOption('base_path') . 'upload/' . $resourceId . '/';
    
        // Add a delay of 5 seconds
    sleep(5);


    // Delete the folders and their contents
    if (is_dir($assetsPath)) {
        $modx->log(modX::LOG_LEVEL_INFO, 'Deleting assets folder for resource '.$resourceId);
        $modx->cacheManager->deleteTree($assetsPath);
    }

    if (is_dir($uploadPath)) {
        $modx->log(modX::LOG_LEVEL_INFO, 'Deleting upload folder for resource '.$resourceId);
        $modx->cacheManager->deleteTree($uploadPath);
    }  
          
    break;
}

I reworked my plugin and now it works for me)

<?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);
    }
}    
    
    
          
    break;
}
1 Like

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.