You can add an if condition in your product-template.liquid file to show items in dropdown only when the variant is available. However, this doesn't work if you have 2 or 3 types of variants. Example: Colour & Size or Colour, Size & Material.
You will need a JS logic, which can get tricky based on theme you are using. Also, you will need to think if all the products go out of stock, what you want to happen?
You can also try out this app: https://apps.shopify.com/autohide-soldout-products
which can hide out of stock variants, and hide entire product when all the variants of products are out of stock. When variant or product is back in stock, it also unhides it from the store. Contact their support team, they are really fast!
@che_t3 you to create new one
here is my solution you can check