Make WordPress Core

Opened 5 years ago

Closed 3 years ago

#53084 closed defect (bug) (invalid)

wp_delete_attachment deletes file even if $force_delete is false

Reported by: pubalacon Owned by:
Priority: normal Milestone:
Component: Media Version:
Severity: normal Keywords: reporter-feedback
Cc: Focuses:

Description (last modified by mukesh27)

Around line 6085 in wp-includes/post.php

line 6085: wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file );

There is no test of $force_delete, so attachment file is physically removed from system file even if not wanted (or definitely not allowed)

Suggested patch:

if (!$force_delete) {
    wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file );
}

Change History (7)

#1 @mukesh27
5 years ago

  • Description modified (diff)

#2 @mukesh27
5 years ago

  • Component GeneralMedia
  • Version 5.7.1

Hi and welcome to WordPress Trac! Thanks for the ticket.

wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ); called in wp_delete_attachment function at #L6125.

Removing version 5.7.1 and set component to Media

#3 @joyously
5 years ago

  • Severity majornormal

Do you have a test case, or were you just reading the code?
The $force_delete variable is checked near the top of the function, and uses an early return, so it matches the description, which says

The attachment is moved to the Trash instead of permanently deleted unless Trash for media is disabled, item is already in the Trash, or $force_delete is true.

  if ( ! $force_delete && EMPTY_TRASH_DAYS && MEDIA_TRASH && 'trash' !== $post->post_status ) {
        return wp_trash_post( $post_id );
  }

#4 @SergeyBiryukov
5 years ago

  • Keywords reporter-feedback added; needs-patch removed

#5 @SergeyBiryukov
5 years ago

Just noting that code similar to the fragment above can be found not only in wp_delete_attachment(), but also in wp_delete_comment() and wp_delete_post(). Perhaps the $force_delete argument in these functions needs better documentation?

This ticket was mentioned in Slack in #core-media by antpb. View the logs.


3 years ago

#7 @antpb
3 years ago

  • Milestone Awaiting Review
  • Resolutioninvalid
  • Status newclosed

Upon review in the Media meeting it was agreed that the docs are pretty clear about the behavior and that this is the intended behavior of the function. If we see more questions around this it would probably be a good move to identify how the docs should be clarified better but for now we should close this to keep the backlog of tickets true.

Note: See TracTickets for help on using tickets.