Delete all pages (thousands) except a few by their IDs

You could do something like this

if (current_user_can('administrator')) {
    $pages = get_posts([
        'post_type' => 'page', // get only pages
        'posts_per_page' => -1, // get all
        'post_status' => array_keys(get_post_statuses()), // all post statuses (publish, draft, private etc...)
        'post__not_in' => [8,18,15,16,17] // list of ids you want to exclude (pages not for deletion)
    ]);

    foreach ($pages as $page) {
        wp_delete_post($page->ID); // delete page (moves to trash)
    }   
}

If you want something quick without working with wp-cli.

I use this sometimes when I need to delete something quickly and once.

You can put this code in your, header.php (will probably get flamed for this XD), and enter your site, this code will only execute for administrator level users.

It will retrieve all pages you want to delete and delete them.

In post__not_in => [pages_ids_here] put all the pages ids you DO NOT want to delete.

wp_delete_post($page->ID) will do a soft delete, move it to trash, if you want to delete the pages permanently the use this wp_delete_post($page->ID, true) (not recommended straight away because you will not have a way to restore them, unless you did a DB backup).

Before you run this code, check what $pages contains to see if you got the correct pages, once you are sure that those are the correct pages you can delete them.

If you can do a DB backup in case something went wrong.

After all pages were deleted remove this code immediately to prevent unwated pages deletion in the future.