NextCloud – Purge Music Library from DB

I recently posted an old bash script I had written to forcefully rescan the music library for the music app in NextCloud when new files aren’t being found, but what happens when you delete or make some changes to the MP3’s or file structure of your music library, then you go to run the rescan library occ script, and NextCloud proceeds to go ape shit, spewing a whole lot of errors in RED, and you can’t do anything? What now, home slice?

Welp, after some research, I found the easiest fix was just to dump the corresponding “oc_music_tracks” table from the database and start fresh. So again, it’s script writin’ time, boyz and girlz!

Word to the wise (and to your mother?): Don’t fuck around here. If you don’t know what you’re doing, and you make a mistake, you could fuck up your database, and to quote South Park, “I’m not your responsibilibuddy” guy. Proceed with caution. Nuff said.

Also, if you’re worried about placing your DB credentials in a file where others can see it, then do not proceed, and just manually purge the table using PHPMyAdmin, or with the MySQL command line.

Same as before, I created “purgemusiclib.sh” just below the live public html directory where the NextCloud installation sits on the web server, so that it’s not accessible externally. Edit the database variables accordingly to match your installation environment.

touch purgemusiclib.sh
chmod 740 purgemusiclib.sh
nano purgemusiclib.sh
#!/bin/bash
# Author: Nathan Thomas
# Date: 04/22/2023
# Purge NextCloud Music Library from DB

# Enter an account that has write privileges to the NextCloud DB
mysql_user='root'
# Enter your DB user's password
mysql_pass='supersecretpasssword'
# Enter the name of your NextCloud database
mysql_db='nextcloud_db'
# Leave this as is
mysql_tbl='oc_music_tracks'

mysql -u${mysql_user} -p${mysql_pass} ${mysql_db} -e"DELETE FROM ${mysql_tbl};"

# uncomment this if you want it to automatically
# run the rescanmusic.sh script we created in the
# other post
#./rescanmusic.sh
Ctrl+X to save
Run as follows:
./purgemusiclib.sh

If you uncommented the last line before, and you’re also using the “rescanmusic.sh” script we created, stop here, you’re done. Otherwise, manually run the rescan script, or rerun the occ music rescan command to resync your music library.

Leave a Reply