#!/usr/bin/env bash # Codex CLI — Uninstaller # Removes Codex CLI binary, config, env vars. # # Usage: sudo bash ucodex_uninstall.sh set -euo pipefail OS="$(uname -s)" IS_MACOS=false [ "$OS" = "Darwin" ] && IS_MACOS=true # Cross-platform sed -i sedi() { if $IS_MACOS; then sed -i '' "$@" else sed -i "$@" fi } GREEN="\033[92m" CYAN="\033[96m" YELLOW="\033[93m" BOLD="\033[1m" RESET="\033[0m" log() { echo -e "${GREEN}[+]${RESET} $*"; } warn() { echo -e "${YELLOW}[~]${RESET} $*"; } info() { echo -e "${CYAN}[i]${RESET} $*"; } echo -e "${BOLD}" echo " +--------------------------------------+" echo " | Codex CLI — Uninstaller |" echo " +--------------------------------------+" echo -e "${RESET}" # ---- Remove binary ---- CODEX_PATH=$(command -v codex 2>/dev/null || echo "") if [ -n "$CODEX_PATH" ]; then info "Removing wrapper: $CODEX_PATH" rm -f "$CODEX_PATH" log "Wrapper removed" else warn "Codex wrapper not found in PATH" fi # Also remove the hidden binary if [ -f "/usr/local/bin/.codex-bin" ]; then info "Removing binary: /usr/local/bin/.codex-bin" rm -f "/usr/local/bin/.codex-bin" log "Binary removed" fi # ---- Determine user home directories ---- if $IS_MACOS; then _home_dirs=(/Users/*) else _home_dirs=(/root /home/*) fi # ---- Remove config ---- for user_home in "${_home_dirs[@]}"; do CODEX_DIR="$user_home/.codex" if [ -d "$CODEX_DIR" ]; then info "Removing $CODEX_DIR..." rm -rf "$CODEX_DIR" log "Removed $CODEX_DIR" fi done # ---- Remove env vars from shell rc files ---- for user_home in "${_home_dirs[@]}"; do for rc_file in "$user_home/.bashrc" "$user_home/.zshrc"; do if [ -f "$rc_file" ] && grep -q 'OPENAI_API_KEY\|OPENAI_BASE_URL\|Codex' "$rc_file" 2>/dev/null; then info "Cleaning env vars from $rc_file..." sedi '/# Codex/d' "$rc_file" sedi '/# UnlimitedCoding.*[Cc]odex/d' "$rc_file" sedi '/OPENAI_API_KEY/d' "$rc_file" sedi '/OPENAI_BASE_URL/d' "$rc_file" log "Cleaned $rc_file" fi done done # ---- Remove env vars from /etc/environment ---- if [ -f "/etc/environment" ] && grep -q 'OPENAI_API_KEY\|OPENAI_BASE_URL' /etc/environment 2>/dev/null; then info "Cleaning /etc/environment..." sedi '/OPENAI_API_KEY/d' /etc/environment sedi '/OPENAI_BASE_URL/d' /etc/environment log "Cleaned /etc/environment" fi # ---- Remove env file and profile.d scripts ---- for envf in "/etc/profile.d/codex-cli.sh" "/etc/profile.d/codex-env.sh" "/etc/codex-env.sh"; do if [ -f "$envf" ]; then rm -f "$envf" log "Removed $envf" fi done # ---- macOS: unset launchctl env vars ---- if $IS_MACOS; then launchctl unsetenv OPENAI_API_KEY 2>/dev/null || true launchctl unsetenv OPENAI_BASE_URL 2>/dev/null || true log "Unset launchctl env vars" fi # ---- Clean codex-env.sh source lines from rc files ---- for user_home in "${_home_dirs[@]}"; do for rc_file in "$user_home/.bashrc" "$user_home/.zshrc"; do if [ -f "$rc_file" ] && grep -q 'codex-env\.sh' "$rc_file" 2>/dev/null; then sedi '/codex-env\.sh/d' "$rc_file" log "Cleaned codex-env source from $rc_file" fi done done echo "" echo -e "${GREEN}${BOLD} Codex CLI fully uninstalled!${RESET}" echo ""