#12108 closed feature request (wontfix)
cant delete attachments when deleting a post
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | General | Version: | 2.9.1 |
| Severity: | normal | Keywords: | |
| Cc: |
Description
Hi.
i want to delete all attached images/videos etc when deleting a post.
it seems there is no solution for it.
simply if a media is attached to a post and post not need anymore, it must be delete too when deleting post.
Change History (2)
- Milestone 2.9.2 deleted
- Resolution set to wontfix
- Status changed from new to closed
comment:2
janeforshort — 3 years ago
As scribu pointed out, the reason is because media files may be used by other posts as well, which is why they must be deleted in the media library. If we changed it so that deleting a file from a post deleted it altogether from the system, it would break the existing behavior and cause a lot of unintentional deletions. Agree with wontfix.
Note: See
TracTickets for help on using
tickets.

An attachment can be used in other posts, without being directly attached to them.
Here's some code I use that does what you want:
function delete_post_children($post_id) { global $wpdb; $ids = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_parent = $post_id AND post_type = 'attachment'"); foreach ( $ids as $id ) wp_delete_attachment($id); } add_action('delete_post', 'delete_post_children');