#!/usr/bin/env bash # # qvm-clean - Completely remove all QVM state, images, and caches # # This script performs a full cleanup of all QVM-related data: # - Base image (base.qcow2) # - VM overlay and state (overlay.qcow2, pid, ssh port, logs, workspaces) # - Build caches (cargo, pnpm, sccache) # - Optionally: user configuration (flake) # # WARNING: This is destructive and cannot be undone! # # Usage: qvm clean [-f|--force] # -f, --force Skip confirmation prompt # set -euo pipefail # Source common library readonly QVM_LIB_DIR="${QVM_LIB_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")/../lib" && pwd)}" source "${QVM_LIB_DIR}/common.sh" # Get path to qvm-stop script readonly QVM_STOP="${QVM_BIN_DIR:-$(dirname "$0")}/qvm-stop" # # confirm_clean - Prompt user for confirmation # Args: $1 - whether to delete config (true/false) # Returns: 0 if user confirms, exits script if user cancels # confirm_clean() { echo log_warn "This will delete ALL QVM data:" echo " - Base image: $QVM_DATA_DIR" echo " - State/overlay: $QVM_STATE_DIR" echo " - Build caches: $QVM_CACHE_DIR" echo " - Config/flake: $QVM_CONFIG_DIR" echo log_warn "This operation CANNOT be undone!" echo "You will need to rebuild the base image from scratch next time." echo read -p "Are you absolutely sure? [y/N] " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then log_info "Clean cancelled" exit 0 fi } # # main - Main cleanup orchestration # main() { local force=false # Parse arguments while [[ $# -gt 0 ]]; do case "$1" in -f|--force) force=true shift ;; *) die "Unknown option: $1" ;; esac done # Confirm unless --force is used if [[ "$force" != "true" ]]; then confirm_clean fi # Stop VM if running if is_vm_running; then log_info "Stopping running VM..." "$QVM_STOP" fi # Delete directories log_info "Removing QVM data directories..." if [[ -d "$QVM_DATA_DIR" ]]; then log_info " - Deleting: $QVM_DATA_DIR" rm -rf "$QVM_DATA_DIR" fi if [[ -d "$QVM_STATE_DIR" ]]; then log_info " - Deleting: $QVM_STATE_DIR" rm -rf "$QVM_STATE_DIR" fi if [[ -d "$QVM_CACHE_DIR" ]]; then log_info " - Deleting: $QVM_CACHE_DIR" rm -rf "$QVM_CACHE_DIR" fi if [[ -d "$QVM_CONFIG_DIR" ]]; then log_info " - Deleting: $QVM_CONFIG_DIR" rm -rf "$QVM_CONFIG_DIR" fi # Print success message echo log_info "QVM cleaned successfully!" echo echo "All QVM data has been removed from your system." echo "Next run of 'qvm start' will initialize everything from scratch." echo } main "$@"