Hello! I've definitely been in your position before. Do you have iCloud syncing enabled for Music? Here's how to check if you do:
- Open Settings
- Tap [Your name] > iCloud > Show All
If the switch next to 'Music' is off/grey, you may be able to restore your music library from an earlier backup.
iPhone automatically backs up to iCloud if:
- It is signed into your Apple ID
- It's able to access a network
- There's enough iCloud storage space
Here's how to restore iPhone from an earlier iCloud backup:
- Erase all data from your iPhone: How to factory reset your iPhone, iPad, or iPod touch - Apple Support
- Follow the onscreen setup steps until you reach the Apps & Data screen, then tap Restore from iCloud Backup.
- Sign in to iCloud with your Apple ID.
- Choose a backup. You should be able to see the date of each backup. Choose the one from before you erased your music.
(Restore your iPhone, iPad, or iPod touch from a backup)
I hope this helps!