[wp-trac] [WordPress Trac] #47868: wp_delete_attachment returning successfully, deleting all DB data, but NOT deleting files, and NOT returning false

WordPress Trac noreply at wordpress.org
Fri Aug 16 22:36:07 UTC 2019


#47868: wp_delete_attachment returning successfully, deleting all DB data, but NOT
deleting files, and NOT returning false
--------------------------+------------------------------
 Reporter:  Jossnaz       |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Media         |     Version:
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+------------------------------

Comment (by donmhico):

 Hello @Jossnaz,

 I cannot reproduce the bug in my end. But if you want to be able to see if
 the files are indeed deleted. You may try this.
 {{{#!php
 <?php
 $attachments = get_attached_media( '', $post->ID );

 foreach( $attachments as $attachment ) {
     $meta = wp_get_attachment_metadata( $attachment->ID );
     $backup_sizes = get_post_meta( $attachment->ID,
 '_wp_attachment_backup_sizes', true );
     $file = get_attached_file( $attachment->ID );

     // Here you'll get either true or false.
     $delete_files = wp_delete_attachment_files( $attachment->ID, $meta,
 $backup_sizes, $file );

     // Still need to call this to delete the `attachment post`.
     wp_delete_attachment( $attachment->ID, true );
 }
 }}}

 Reference:
 https://developer.wordpress.org/reference/functions/wp_delete_attachment/

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/47868#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list