Anyone who runs a script as root which deletes files is asking for trouble.
So, when the OP asks "
what went wrong?", the answer is pretty straight forward.
The OP ran a script that removes files (as the superuser!) without testing the script first. It's that simple.
How do you test it?
Well, when the script runs, instead of removing the files, you simply write the files that would be deleted to a file. Then, you examine the file with the output of "files and directories that would have been deleted" and make sure the script does what you want. If all is OK,
then enable the script to actually work (delete the files).
Never, ever, run a script that deletes files (especially as root) without testing first and confirming the script is working as intended.
Edit: Note that ZB recommends a similar approach in
his reply above.