Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
It seems that images added to a node via an image field become permanently un-deleteable, even if they are removed from that node, and even if that node is then deleted. This seems to occur any time an image is attached to a node, regardless of the initial upload method. That is, if an image is uploaded via the media listing page, then attached to a node, it will subsequently be stuck in the system even if removed from the node.
Comment | File | Size | Author |
---|---|---|---|
#10 | 1240834_10_media_forced_delete.patch | 1.6 KB | szantog |
Comments
Comment #1
aspilicious CreditAttribution: aspilicious commentedI had the same problem with undeletabled files and images. Frustrating
Comment #2
tsvenson CreditAttribution: tsvenson commentedSeems this might also be a problem with user pictures. On my test site, the first user picture wasn't uploaded correct, thus not shown either on the user page or as thumbnail in the media browser. After uploading a working picture, I tried to use the media browser to delete the old one (really just a file name viewed), but media browser said it couldn't because the file was in use.
Comment #3
idflood CreditAttribution: idflood commentedI've tried to reproduce the error without luck. This was on a fresh drupal 7 + media-2.x + dependencies installed with git. Here are the steps I tried
Did I miss something?
Comment #4
aspilicious CreditAttribution: aspilicious commentedMaybe this is caused by an upgrade from an older version of media.
Comment #5
Anonymous (not verified) CreditAttribution: Anonymous commentedI suppose it's possible this is caused by an upgrade issue. I did upgrade from the 1.x branch. However, I've tested by completely uninstalling all media related modules and reinstalling, which is about as close as I can get to a fresh install. No dice.
I thought it might be possible this is caused by some sort of other module conflict. I tried uninstalling Entity Cache to see if there was some weird caching thing going on, but that doesn't seem to be involved.
Digging into MySQL I've discovered that my trouble files are present in the file_usage table and listed as being in use by the nodes in question. This means they are not being properly removed from the file_usage table, and not simply being mis-identified as being in use after the fact. In fact, if the image is then re-added to the node, the usage count is upped by one. I have numerous usage counts at 5+ per node, which generally seems impossible unless the client has been adding the same image to the body copy multiple times.
I have the feeling (though I'm not positive) the problem can be traced to line 381 of media.fields.inc where the usage count is supposed to be reduced by one. Clearly, this doesn't seem to be happening, though I'm unsure exactly which check is failing.
Comment #6
Anonymous (not verified) CreditAttribution: Anonymous commentedAfter much investigation I have figured out this is caused by the revision system in core. Every time a new revision is created for an entity, the usage count in the
file_usage
table tying that entity and any file which happens to be on it at the time increases by one. If you delete the file, save the entity, and make a new revision, the usage count will not decrease because that count and file are still tied to older revisions of the entity. This happens in both thefile_field_update
andmedia_field_update
functions.The only way to remove files which are tied to an entity with revisions is to delete all older revisions for that entity, or to manually go in and reduce the usage count in the database. Deleting the entity entirely doesn't actually seem to clear the usage count.
So this is not actually a Media issue, it's a core issue. Or at least, it's an issue when the default revision handling is combined with the Media module. Hard drive space concerns aside, I suppose it makes sense to keep an image around if there are older versions of a entity using it. Using the default file field setup, you really have no idea what's happening with your files under the hood and it doesn't really affect the user much if there's hundreds of files stacking up.
However, when using Media, all those leftover files are just going to keep piling up in the Media browser and you'll never be able to do anything with them unless you go back and delete all the entity revisions which are keeping them in the system. This seems to be far less than ideal.
A solution might be to check every
field_revision_field_NAME
table in the database and compare file IDs to see if which items have been deleted from the current revision of the entity they are attached to. These items could then be hidden from the Media browser. If you reverted to a previous revision of an entity they would be shown again.A potential downside of this method might be the large number of lookups required (I assume) and the fact that each field seems to have a separate revision table in the database. Another potential downside might be user confusion over why files they thought they removed mysteriously reappear at a later time (A better problem to have than un-deleteable files though).
For the moment, my solution is probably going to be to turn off revisions and remove all older revisions so my users can clear out their Media library. They'll lose the ability to revert back to older content but at least they'll be able to mange their assets how they're expecting.
Comment #7
mlncn CreditAttribution: mlncn commentedJust did much of this investigation myself. here's the keyword text i couldn't find before: "The file X is in use and cannot be deleted."
The
if (!empty($entity->revision)) {
check is always true, in my tests, and then the return statement means the file_usage_delete() function is never called. This is because revisions are enabled for the relevant content type.Moving this code down 'fixes' the use case when a file is removed from a node before there are any revisions. But that's by pretending the file is no longer associated with the revision that just got saved. Drag.
So long as files are used by revisions, it makes sense that they need to exist. If the admin list could be filtered by 'active' images that would be great but seems that would be hard to do.
Will look into Views for alternate admin UI that doesn't have to show all images.
Comment #8
wmostrey CreditAttribution: wmostrey commentedI'm running into the same issue, and it's also happening in 7.x-1.0-beta5. The solution we come up with for this problem should be back-ported.
Related issue: #892352: Confusing things happen when you click "Delete" or "Cancel" on the Media delete form starting from comment #6.
Comment #9
fictionindustries CreditAttribution: fictionindustries commentedStill broken with 7.8 release :(.
subscribing
Comment #10
szantog CreditAttribution: szantog commentedI think, I could solve this.
Yesterday worked on that: #1274524: More useful warning message, if media isn't deletable. I created an error message for this case too.
Today I've figured, why just print the nid, what doesn't exist, why don't work with it?
Sorry, I mixed a little bit this with my previous issue, but I think, this is nearby connection with that.
I think, if the the content doesn't exist, we should force to delete the media.
Comment #11
szantog CreditAttribution: szantog commentedThis isn't perfect, it only works, when content is deleted.. Unfortunatly, there is a core issue: #1191438: file_usage not updated after removing a file/image field
Comment #12
wmostrey CreditAttribution: wmostrey commentedShouldn't we just make this a duplicate of #1191438: file_usage not updated after removing a file/image field then?
Comment #13
nlambert CreditAttribution: nlambert commentedsubscribe
Comment #14
CarbonPig CreditAttribution: CarbonPig commentedsubscribe - same issue
Comment #15
amateescu CreditAttribution: amateescu commentedAgreed with #12, let's close this as a duplicate of #1191438: file_usage not updated after removing a file/image field.
Comment #16
Leeteq CreditAttribution: Leeteq commentedGood to keep this one open until the core bug is fixed.
Then, if nothing needs to be adjusted in Media module after that core bug fix, then this issue can be closed.
Setting as Postponed, awaiting #1191438: file_usage not updated after removing a file/image field
Comment #17
Anonymous (not verified) CreditAttribution: Anonymous commentedI'm not sure this is actually a duplicate of that particular issue. It's true that this problem will occur if a file field is removed from an entity type, but it will also occur if a file is simply removed from a node or swapped out if revisions are turned on. I personally believe this is probably an intentional feature in core so you can roll back a node to a previous revision complete with the image/file that was included in that revision.
That means that even after the file_usage bug is fixed there will probably still be old files in the system which are attached to old node revisions. If this is the case, it still seems that Media is going to have to basically filter these out of the Media Library view.
Comment #18
carole CreditAttribution: carole commentedHow can you trace where the image is used? Does 7.x.2.x.dev post the NID of the file where the image is used? I'm on 7.x-1.0-beta4.
Comment #19
Anonymous (not verified) CreditAttribution: Anonymous commentedThe Media module doesn't currently expose this information as far as I'm aware. That information is in the database in the file_usage table, so it's possible to determine where a file is being used it's just not visible in Media module yet.
Comment #20
brunodboCould this be related to #911830: Can't delete any media item, because all are supposedly in use?
Comment #21
rgracia CreditAttribution: rgracia commentedSubscribe
Comment #22
emptyvoid CreditAttribution: emptyvoid commentedThis may or may not be related but I get a notice and the following error when attempting to delete any file type with the media browser.
Comment #23
fonea CreditAttribution: fonea commentedyou could try this :
~~~~
$entity = entity_load_single($entity_type, $entity_id);
$entity->field_image = array();
entity_save($entity_type, $entity);
where 'field_image' is the field Name.
~~~~
thanks.
Comment #24
Rontero CreditAttribution: Rontero commentedWhat I'm about to post is not the best solution. It might work for some people, though.
My setup: drupal 7.12 with 50+ modules installed (so thers is no point in lisiting them). The one, I thing makes all the difference, is File Lock.
Using media and media browser plus I uploaded many pics which were then linked to taxonomy terms. Later on I locked them with previously mentioned file lock module.
Since I didn't need those photos any more I had to delete theme. Shortly after I encountered problem described in this issue. Deleting terms did not let me delete pictures. However, for some reason after unlocking them I was able do actually delete them. I kept getting errors, but still, the process worked.
Comment #25
drupwash CreditAttribution: drupwash commented#10: 1240834_10_media_forced_delete.patch queued for re-testing.
Comment #27
dimitriseng CreditAttribution: dimitriseng commentedI have the same issue, is there a proper patch or do we still need to wait on the core patch? Is this only for media 2.x or is this going to be backported to 1.x? Thanks.
Comment #28
arthurf CreditAttribution: arthurf commentedThis thread: #1268116: WYSIWYG does not record file usage attempts to handle the file usage for node deletion and node revision deletion. If this issue persists we should look at the file_usage table to see what modules are blocking the delete.
Comment #29
Dave ReidComment #30
Dave ReidRelated:
#1968442: RFC: How to prevent files from being deleted by file/media fields when unused
Comment #31
desiboli CreditAttribution: desiboli commented10: 1240834_10_media_forced_delete.patch queued for re-testing.
Comment #34
Chris Matthews CreditAttribution: Chris Matthews as a volunteer commentedClosing this issue as outdated. However, if you think this issue is still important, please let us know and we will gladly re-open it for review.
sincerely,
- the Drupal Media Team