If you plan to sell or give away your iPhone or iPad, here is an article that explains how to do it safely and securely.
I have never heard of, or used, the program iMazing, which is used in the article, and costs $40 for a single license. If you don't want to purchase and use iMazing (and I probably won't), then do a backup to iCloud and follow the rest of the tips in the article, which are quite helpful.
If you browse through the iMazing website there are many other functions available from this program.
Here is a link that will take you to an FAQ website that explains a lot more about this program, should you have an interest.