If you are using Linux
You can test if a directory is a mount point using mountpoint.
To avoid mount point appearing/disappearing between test and rm -r, you need to run the script in a separate mount namespacemount namespace with private subtreeprivate subtree (mounts do not propagate from/to new name spacenamespace). This can be accomplished with unshare.
unshare -m --propagation private -- "<delete script>"
Everything in same script:
#!/bin/sh
unshare -m --propagation private -- sh -e <<EOF
if ! mountpoint -q "<path>"; then
rm -r "<path>"
fi
EOF