Old message threads quietly collect photos and videos, and nobody ever goes back to clean them. Messages has a setting that does the cleaning on a schedule. It also has a catch big enough that I’d read this before turning it on.
Settings > Apps > Messages > Keep Messages
The setting sits under Message History. Pick an option and your messages and attachments older than the period you chose are deleted on an ongoing basis. Apple’s guide describes deleting messages and attachments that are older than 30 days or a year, and Forever is the option that keeps everything.
The catch is in Apple’s own warning: choose anything other than Forever and conversations are removed automatically after the time you set, and that includes all attachments. There is no archive step in between. If a conversation holds a photo or a confirmation you need later, save it before the timer gets to it.
Manually deleted messages are another matter. They stay in the Recently Deleted folder for up to 30 days, and you can recover them during that time.
If you only want to clear out the heavy stuff, do it by hand instead: tap the contact or group icon at the top of a conversation, choose a category such as Photos, tap Edit, then Select, pick the attachments, tap Delete and confirm the count. To remove one item, touch and hold the bubble or attachment, tap More, select it and tap the Delete button.
One more caveat: the setting is under Settings, Apps, Messages on iOS. Apple’s page says nothing about what happens to copies on your other devices, so I’d not treat it as a way to clean up a Mac or an iPad.
Further reading: Delete messages and attachments in Messages on iPhone, Delete messages and conversations in Messages on Mac
Going deeper: Guide: Immich on Unraid, back up the photos on your iPhone automatically