Back to Resources

Level Verified

macOS Disk Cleanup Script

Created by

Level

Type

Script

Category

Maintenance

Platforms
WindowsApple iOSLinux

Problem Overview

Running out of disk space on macOS can lead to slow performance, application issues, and user frustration. Manually removing temporary files, caches, and outdated downloads is time-consuming. This script automates those cleanup tasks, helping you reclaim valuable storage and enhance overall system health.

Description

This script searches key locations on macOS—like temporary directories, browser caches, and old download folders—then systematically removes files that are likely no longer needed. It also clears system logs and purges leftover developer data, all while logging its actions for easy review. By targeting files older than a specific age and skipping any that might still be in use, the script ensures a targeted, safe cleanup process that frees up space without risking critical system files.

You can integrate this script into Level for on-demand cleanups. Simply configure a script-based monitor to trigger the script automatically whenever disk space falls below a certain threshold, or use a scheduled automation to run it periodically.

Script

#!/bin/bash

# This resource is provided as a convenience for Level users. We cannot 
# guarantee it will work in all environments. Please test before deploying 
# to your production environment. We welcome contributions to our community 
# library

# Level Library
# https://level.io/library/script-macos-disk-cleanup

# Create log file
LOG_FILE="/tmp/cleanup_$(date +%Y%m%d_%H%M).log"
TOTAL_SAVED=0

log_message() {
    echo "$1" | tee -a "$LOG_FILE"
}

get_size() {
    if [[ -e $1 ]]; then
        du -sk "$1" 2>/dev/null | cut -f1
    else
        echo "0"
    fi
}

delete_safely() {
    local path="$1"
    local desc="$2"
    
    if [[ -e "$path" ]]; then
        local size_before=$(get_size "$path")
        rm -rf "$path" 2>/dev/null
        local freed=$((size_before))
        TOTAL_SAVED=$((TOTAL_SAVED + freed))
        log_message "Cleaned $desc - Freed $((freed/1024)) MB"
    fi
}

log_message "Cleanup started at $(date)"

# Clean various temp directories
TEMP_PATHS=(
    "/tmp/*"
    "/private/tmp/*"
    "/private/var/tmp/*"
    "$HOME/Library/Caches/*"
    "$HOME/Library/Logs/*"
    "$HOME/Library/Safari/Downloads/*"
)

log_message "\nCleaning temporary files..."
for path in "${TEMP_PATHS[@]}"; do
    find "$path" -mindepth 1 -mtime +1 -print0 2>/dev/null | while IFS= read -r -d '' file; do
        if [[ ! -f "$file" ]] || lsof "$file" >/dev/null 2>&1; then
            continue
        fi
        delete_safely "$file" "temp file: $(basename "$file")"
    done
done

# Clean Downloads folder
log_message "\nCleaning Downloads folder..."
if [[ -d "$HOME/Downloads" ]]; then
    find "$HOME/Downloads" -mindepth 1 -mtime +30 -print0 2>/dev/null | while IFS= read -r -d '' file; do
        if [[ ! -f "$file" ]] || lsof "$file" >/dev/null 2>&1; then
            continue
        fi
        delete_safely "$file" "old download: $(basename "$file")"
    done
fi

# Clean Chrome Cache (if Chrome is installed)
if [[ -d "$HOME/Library/Caches/Google/Chrome" ]]; then
    log_message "\nCleaning Chrome Cache..."
    delete_safely "$HOME/Library/Caches/Google/Chrome/Default/Cache/*" "Chrome cache"
fi

# Clean system logs
log_message "\nCleaning system logs..."
sudo rm -rf /private/var/log/*.log* 2>/dev/null
sudo rm -rf /private/var/log/asl/*.asl 2>/dev/null

# Clean XCode derived data (if XCode is installed)
if [[ -d "$HOME/Library/Developer/Xcode/DerivedData" ]]; then
    log_message "\nCleaning XCode derived data..."
    delete_safely "$HOME/Library/Developer/Xcode/DerivedData" "XCode derived data"
fi

# Clean App Store cache
log_message "\nCleaning App Store cache..."
delete_safely "$HOME/Library/Caches/com.apple.appstore" "App Store cache"

# Final summary
TOTAL_GB=$(echo "scale=2; $TOTAL_SAVED/1024/1024" | bc)
log_message "\nCleanup completed at $(date)"
log_message "Total space saved: ${TOTAL_GB} GB"
log_message "Log file saved to: $LOG_FILE"

# Empty trash
log_message "\nEmptying Trash..."
rm -rf ~/.Trash/* 2>/dev/null

# Clear system cache
sudo purge 2>/dev/null

Use Cases

  • Quick recovery of disk space on macOS endpoints
  • Routine cleanup in MSP environments to maintain performance
  • Automating maintenance tasks for end-users or project-based systems
  • Integrating with Level monitors to proactively clear space when capacity is low

Recommendations

  • Test in a non-critical environment before wide deployment
  • Set up a script-based monitor in Level to automatically trigger when disk space dips below a threshold
  • Schedule periodic cleanups using a Level automation for ongoing maintenance
  • Review logs generated in /tmp to confirm proper cleanup
  • Customize target folders in the script if your organization stores files in unique locations

FAQ

  • Will this script delete important user files?
    The script targets specific directories known for temporary or old data. It avoids files in use by checking for open file handles, but always test first to confirm it suits your environment.
  • What happens if the script encounters locked files?
    Locked or in-use files are skipped to prevent errors or data loss.
  • How often should I run this script?
    It depends on your usage. Some MSPs schedule it weekly, while others tie it to a disk-space monitor in Level for on-demand triggers.
  • Can I modify which directories get cleaned?
    Absolutely. You can add or remove directory paths in the script to align with your organization’s file management policies.
  • Is the log file saved permanently?
    The log file is placed in /tmp with a date stamp. It will remain there until manually removed or cleared by your system’s temp file policies.

Included with this Script:

Below is a list of what you can expect to find when importing this Script.

Script details:

The following data and settings will be imported with your script.

Script Name

macOS - Cleanup Disk Space

Description

This script performs a comprehensive cleanup of temporary files, logs, caches, and other unused data on a macOS system to free up disk space. It logs all cleanup actions, calculates the total space saved, and outputs a detailed summary in a timestamped log file.

Language

Bash

Timeout (In Seconds)

300

Run As

Local system

Import into Level

Related resources

Explore more automations, scripts, and policies to further enhance your IT operations.

View all resources