Just for clarity: borrowing from this answer, add the following to your theme’s functions.php
:
function wpse_188427_delete_post_media( $post_id ) {
$attachments = get_posts(
array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_status' => 'any',
'post_parent' => $post_id,
)
);
foreach ( $attachments as $attachment ) {
wp_delete_attachment( $attachment->ID );
}
}
add_action( 'before_delete_post', 'wpse_188427_delete_post_media' );
// Uncomment the following line if you also want to delete media when the post is trashed
// add_action( 'wp_trash_post', 'wpse_188427_delete_post_media' );