If you have an iPhone, just do like you do for phone calls.
Set up your contacts completely, go to messages and select "unknown" and send them to that trash bin. Then go to notifications and either block or not. Apple will delete after a month.
However, just be aware that unless you leave notifications on or check your trash, you can miss something perhaps. Or not 🤣
Like always you can also block an individual number, but that turns into a loser's game.