If you change your iCloud settings for Messages to "Keep Messages for 30 days," this will delete messages older than 30 days from all your devices connected to that iCloud account, including your iPhone. This occurs because enabling Messages in iCloud means all your message data is synchronized across your devices. Therefore, changes made in your iCloud settings—like deleting messages after a certain period—will be universally applied across all your synced devices.
Apple currently does not provide an option to keep messages indefinitely only on your phone while automatically removing them from iCloud. If you want to keep messages forever on your phone but not store them permanently in iCloud, you'll need to disable Messages in iCloud. To do this, navigate to your Settings app, tap your name at the top, select iCloud, and then toggle Messages to the "OFF" position. You'll receive a prompt; choose "Disable and Download Messages" to ensure all current messages remain safely stored locally on your phone.
Additionally, to ensure your messages are preserved indefinitely on your device, it's a good practice to adjust your local message retention settings. Go to Settings, select Messages, and under the "Keep Messages" option, select "Forever." This ensures your phone won't automatically delete older messages.
However, keep in mind that by disabling Messages in iCloud, your texts will no longer be synchronized or automatically backed up to iCloud. Therefore, if backing up your messages is important, consider performing manual backups periodically through a computer using iTunes on Windows or macOS Mojave (or earlier), or Finder on macOS Catalina and newer.
In short, if your goal is to keep your messages permanently stored on your phone without having them deleted automatically, you must avoid using the 30-day deletion setting with Messages in iCloud.