Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Element Delete Action API doesn't support hard deleting #7901

Closed
BenParizek opened this issue May 8, 2021 · 2 comments
Closed

Element Delete Action API doesn't support hard deleting #7901

BenParizek opened this issue May 8, 2021 · 2 comments
Labels

Comments

@BenParizek
Copy link
Contributor

Description

If I attempt to implement the Delete Action in a custom Element and set it to hard delete:

protected static function defineActions(string $source = null): array
{
    $actions = parent::defineActions();

    $actions[] = Craft::$app->getElements()->createAction([
        'type' => Delete::class,
        'hard' => true,
    ]);

    return $actions;
}

The action succeeds but the element is only soft deleted in the database. This appears to be because the selected items are soft deleted on line 188 and then re-queried on line 194, which returns zero IDs because the query does not find the items that were just soft-deleted above:

foreach ($query->all() as $element) {
if (!$element->getIsDeletable()) {
continue;
}
if (!isset($deletedElementIds[$element->id])) {
if ($withDescendants) {
foreach ($element->getDescendants() as $descendant) {
if (!isset($deletedElementIds[$descendant->id]) && $descendant->getIsDeletable()) {
$elementsService->deleteElement($descendant);
$deletedElementIds[$descendant->id] = true;
}
}
}
$elementsService->deleteElement($element);
$deletedElementIds[$element->id] = true;
}
}
if ($this->hard) {
$ids = $query->ids();
if (!empty($ids)) {
Db::delete(Table::ELEMENTS, [
'id' => $ids,
]);
}
}

In looking into the issue I also noticed that this version of the hard delete does not take the extra step of deleting the related search index data, as the hard delete behavior in the service layer does:

cms/src/services/Elements.php

Lines 1525 to 1530 in ba3b1e7

Db::delete(Table::ELEMENTS, [
'id' => $element->id,
]);
Db::delete(Table::SEARCHINDEX, [
'elementId' => $element->id,
]);

@brandonkelly
Copy link
Member

Thanks for reporting! Merged your PR for the next release.

@brandonkelly
Copy link
Member

Craft 3.6.14 is out now with that fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants