Problem

I could be wrong but it looks like file is not deleted after the corresponding file entity was deleted.

How to reproduce

1) Create a node with file attached via File Entity.
2) Delete the node.

Expected result

The node is deleted.
The file entity is deleted.
The file itself is deleted.

Real result

The node is deleted.
The file entity is deleted.
The file itself is NOT deleted.

Solution

I guess we should implement hook_entity_delete in order to achieve it, something like this:

/**
 * Implements hook_entity_delete().
 */
function file_entity_entity_delete($entity, $type) {
  if ($type != 'file') {
    return;
  }
  file_usage_delete($entity, 'file');
  file_delete($entity);
}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

PatchRanger created an issue. See original summary.

PatchRanger’s picture

Status: Active » Needs review
FileSize
892 bytes

Status: Needs review » Needs work

The last submitted patch, 2: 2629120-file_entity-implement_hook_entity_delete.patch, failed testing.

PatchRanger’s picture

It looks weird that those quite unrelated unit-tests get failed by this patch. It seems like the logic of replacing a file relies on some side-effect, which erroneously becomes broken after introducing the changes of the patch - or I am missing something?

PatchRanger’s picture

Priority: Normal » Major

Marking as major as it leads to needless costs when using together with AWS.

Dave Reid’s picture

Category: Bug report » Support request
Priority: Major » Normal
Status: Needs work » Postponed (maintainer needs more info)

I see that both file_delete() (provided by core) and file_delete_multiple() (provided by file entity) attempt to call file_unmanaged_delete() on the files. The only reason I would think of this happening is if file_valid_uri() is returning FALSE. Is this happening only for specific files like ones using the Amazon S3 stream wrapper?

PatchRanger’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)

Hello Dave, thank you for getting in!

Is this happening only for specific files like ones using the Amazon S3 stream wrapper?

We're using AmazonS3 module, which provides custom stream wrapper. Default download method is set to it (option called "Amazon Simple Storage Service") site-wide. So, answering your question, yes, in our case it only relates to that stream wrapper.

I have checked whether File Entity is properly patched as described at AmazonS3 project page - and discovered that one of the required patches is missing: #2479483: Add hook_file_entity_upload_destination_uri_alter() for remote stream wrapper / s3 support. Looks like in our case drush make failed to apply it - and I missed it.
Sorry for bothering - and thank you for assistance!

justafish’s picture

@PatchRanger that patch and the instructions on the module's d.o page only apply to the 2-x branch. Check the README for the 1-x branch for any relevant instructions, but I suggest you upgrade to the new version if possible.

PatchRanger’s picture

@justafish Thank you for assistance! I am going to upgrade to 7.x-2.x.

PatchRanger’s picture

Status: Closed (works as designed) » Active

It seems upgrading didn't help, the problem persists.

Problem (described Behat-style :) )

Given File Entity module is installed
And Composer Manager module is installed
And AmazonS3 module is installed
And all required by AmazonS3 patches are successfully applied
And Status Report has no errors
And "Amazon Simple Storage Service" is set as default download method
And I create a node
And I upload a unique (to be sure it is not used anywhere else) file as a file entity of the node
And a file is for sure available for download from AWS
When I delete the node
Then the file is still available for download
(But the file must not be available for download)

Environment

PHP 5.5.9-1ubuntu4.11
Drupal 7.41 (install profile OpenAtrium 7.x-2.50)
File Entity 7.x-2.0-beta2
Composer Manager 7.x-1.8
AmazonS3 7.x-2.1-beta1

I see that both file_delete() (provided by core) and file_delete_multiple() (provided by file entity) attempt to call file_unmanaged_delete() on the files.

@Dave Reid I see your point, it's reasonable. Could it be the case that file_delete() is not being called at all? I mean, if it would be called, then everything is ok, - but what if deleting the entity doesn't trigger file_delete()? Do you see the point of the patch? Does it make sense? Why do you think it fails?

PatchRanger’s picture

Category: Support request » Bug report
Priority: Normal » Major
Status: Active » Needs work

Again, marking as major as it leads to needless costs when using together with AWS.
Correct me if I am wrong.

Status: Needs work » Needs review
PatchRanger’s picture

PatchRanger’s picture

Status: Needs review » Needs work
PatchRanger’s picture

PatchRanger’s picture

Status: Needs work » Needs review
PatchRanger’s picture

@Dave Reid , @justafish Looks like I've found the reason. file_delete() invokes both file_delete & entity_delete hooks before file_unmanaged_delete(). It seems it is intended to reduce the counter here by calling to file_usage_delete. But - hey! - here is how File module implements hook_file_delete:

/**
 * Implements hook_file_delete().
 */
function file_file_delete($file) {
  // TODO: Remove references to a file that is in-use.
}

See? It does nothing to unlink existing files. We should add file_usage_delete to some hook implementation. And here is where the question appears: where we should add it? I see 3 options:
- In File module: replace that @todo with file_usage_delete($file, 'file');
- In File Entity: implement hook_file_delete
- In File Entity: implement hook_entity_delete
What do you think? Which hook implementation should call file_usage_delete?

PatchRanger’s picture

I've finally debugged it. The solution is 2 patches: #13 from this issue & #5 from #2639230: After entity was removed the file should be removed too. See solution description in that issue. I've tried both patches separately - they work only together, so it looks like they are both necessary.

joseph.olstad’s picture

Nice patch, I'll review it more thoroughly soon.