You can try the script bellow. But if your volume got into Error, I would recommend you to verify the volume before reusing it. This approach can led your backups into the same error if reusing a physically damaged volume.
#!/bin/bash
#
DBHOST=localhost
DATABASE=bacula
USER=bacula
PASSWORD=""
ERROR_VOLS=($(mysql --host=$DBHOST --database=$DATABASE --user=$USER --password=$PASSWORD -N -e "SELECT VolumeName from Media where VolStatus=\"Error\""))
for ((i=0; i<${#ERROR_VOLS[*]}; i++)) do
echo purge volume=${ERROR_VOLS[i]} | /opt/bacula/bin/bconsole > /dev/null
if [ $? -eq 0 ]; then
printf "Volume %s purged\n" ${ERROR_VOLS[i]}
else
printf "Volume %s not purged\n" ${ERROR_VOLS[i]}
fi
done
Best regards,
Ana