Problem/Motivation

If you delete a file that with a usage count > 0, the referring entities are deleted, instead of the references to them.
The message displayed is 'The reference from entity type %ref_type for file %file_name has been deleted.' which implies that the referring entity itself should not be deleted, only the reference to it.

Relevant code from FileDeleteForm.php:

      // Remove existing usage for the file.
      if (isset($usages['file'])) {
        foreach ($usages['file'] as $type => $entities) {
          foreach ($entities as $id => $usage_count) {
            $ref_entity = $this->entityTypeManager->getStorage($type)->load($id);
            if (!empty($ref_entity)) {
              $ref_entity->delete();

              $this->messenger()
                ->addMessage($this->t('The reference from entity type %ref_type for file %file_name has been deleted.', [
                  '%ref_type' => $ref_entity->getEntityType()->id(),
                  '%file_name' => $this->entity->getFilename(),
                ]));
            }
          }
        }
      }

Setting to critical as involves unexpected deletion of content.

Steps to replicate:
1. Add an image field to node entity
2. Create node "Test node" with image field populated
3. Visit /admin/content/files and click delete for the image added to "Test node"
4. Check both boxes for "Do you want to delete the file immediately?" and "Do you want to force this file to be deleted?"
5. Click "Delete file"

Expected:
Reference to "Test node" is removed

Actual:
The entire entity "Test node" is removed

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

jonnyhocks created an issue. See original summary.

nicrodgers’s picture

Version: 3.0.2 » 3.0.x-dev
Issue summary: View changes

Updated the IS and version.

cobblestone.consulting’s picture

I also ran into this issue and have created a patch that does the following:

Replaces the parent entity delete with a a new Event that gets triggered: FileDeleteEvents:FILE_PRE_FORCE_DELETE. This allows modules that have added usage records to subscribe and add handling for cleaning up the references. The patch includes 2 event subscribers for starters, one for File and one for Media. The Media subscriber runs first and clears any references to the media (from entity references in Fieldable entities) and then deletes the media item. The file subscriber similarly clears any references to the file (except from media) from entity references in Fieldable entities and then removes the usage record (both media and non-media).

At the moment, this still leaves open the possibility (likelihood?) that there will be orphaned references. I think this is a risk that the user is accepting if they are using this functionality in the first place, but it would still be nice to minimize this risk by creating additional Event Subscribers that handle usage references from other modules. "editor" comes to mind as the first one.

smustgrave made their first commit to this issue’s fork.

  • smustgrave committed f14b4be0 on 3.0.x
    fix: #3576737 Deleting a file deletes the node it's used on
    
smustgrave’s picture

Status: Active » Fixed

Going through my projects for D12 and came across this. I'm also updating my settings so I get notified for new issues so I see this faster. Anyway pushed a fix with test coverage. Claude was used to help write the test. Planning a release soon.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.