If you set up WhatsApp to create a backup, you have the option to recover deleted messages on the app.
If you need to reset your iPhone to factory settings to resolve an issue, you can back it up to iCloud first and then restore it to keep your data.