This article explains how to retrieve deleted text messages and iMessages, and possibly recover messages that you've lost. Instructions apply to iOS 11 and later.
How to Recover Deleted Messages Conversations on Your iPhone | Apple Support
The easiest way to get back deleted text messages is in iOS 16. From the moment you delete a text message, you have 30 to 40 days to recover the message.
Select Filters on the Conversations screen in Messages.
Tap Recently Deleted.