you can try this
$all_posts = get_posts(array(
\'numberposts\' => - 1,
\'post_status\' => \'any\',
\'post_type\' => get_post_types(\'\', \'names\') ,
));
foreach($all_posts as $all_post) {
delete_post_media($all_post->ID);
}
function delete_post_media($post_id)
{
if (!isset($post_id)) return;
elseif ($post_id == 0) return;
elseif (is_array($post_id)) return;
else {
$attachments = get_posts(array(
\'post_type\' => \'attachment\',
\'posts_per_page\' => - 1,
\'post_status\' => \'any\',
\'post_parent\' => $post_id
));
foreach($attachments as $attachment) {
if (false === wp_delete_attachment($attachment->ID)) {
// Log failure to delete attachment.
}
}
}
}