Skip to content

Instantly share code, notes, and snippets.

@basebandit
Last active November 22, 2022 14:13
Show Gist options
  • Save basebandit/f591f5b68f3df9b5170f8955248c85ee to your computer and use it in GitHub Desktop.
Save basebandit/f591f5b68f3df9b5170f8955248c85ee to your computer and use it in GitHub Desktop.
#!/bin/bash
function delete_container() {
if ! which "lxc" >/dev/null 2>&1; then
echo "lxc is not installed"
fi
OUT=$(lxc list -c n --format=json | jq 'length')
if [ "$OUT" -lt 1 ]; then
echo "No containers found"
fi
OUT=$(lxc stop --all 2>&1)
check_exit_code $?
containers=$(lxc list --format=json | jq -r '.[] | .name')
for container in $containers; do
echo "Removing container ${container}"
lxc delete "$container" 2>&1
check_exit_code $?
done
}
function check_exit_code() {
status=$1
if [ $status -ne 0 ]; then
echo "{$OUT}"
fi
}
delete_container
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment