Next code will delete the media attached to the post you are deleting!
IMPORTANT: Keep in mind it will only be called when the posts are permanently deleted from the trash! If you want to change this behaviour, include the following line in your wp-config.php file:
define('EMPTY_TRASH_DAYS', 0);
If this constant is set to 0, the trash functionality will be disabled and the ‘Delete Permanently’ button will appear instead of ‘Trash’ button. If you click ‘Delete Permanently’ button, the item will immediately be deleted without any alert message.
Add to functions.php of your theme (child theme preferible)
add_action( 'before_delete_post', function( $id ) {
$attachments = get_attached_media( '', $id );
foreach ($attachments as $attachment) {
wp_delete_attachment( $attachment->ID, 'true' );
}
} );