I want to delete the post attachments too from the 'Media" panel once a custom post is deleted. The post itself gets deleted well, but its media still remains in the wordpress "media" panel:

Archive page:


if (isset($_POST['delete-btn'])) {

    $deletePost = $_POST['post-id'];

    wp_delete_post( $deletePost, true);
    do_action( 'delete_post', $_POST['post-id'] );


//delete all media attached with deleted post
    add_action( 'delete_post', 'delete_post_children' );

    function delete_post_children( $post_id )
    wp_delete_attachment( $post_id );
//delete all media attached with deleted post end

Please help. Thanks.

  • I think that this do_action( 'delete_post', $_POST['post-id'] ); is not necessary, try commenting this line and tring again, also do a print_r to $_POST to check that all the properties exist and have the expected value Jul 14 at 11:47
  • If you want to permanently delete an attachment, try setting the second parameter to true: wp_delete_attachment( $post_id, true ).
    – Sally CJ
    Jul 14 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.