Delete Files by Size on macOS
Learn how to find and delete large files on Mac devices based on size criteria. This helps free up storage space and improve device performance when storage is running low.
List Large Files
Find and list files larger than a specified size:
#!/bin/bash
# Get current user context
CURRENT_USER=$(stat -f "%Su" /dev/console)
CURRENT_USER_UID=$(id -u "$CURRENT_USER")
# Configuration
SEARCH_PATH="/Users/$CURRENT_USER/Desktop"
SIZE_LIMIT="100" # Size in kilobytes
echo "Searching for files larger than ${SIZE_LIMIT}KB in: $SEARCH_PATH"
# List large files
launchctl asuser $CURRENT_USER_UID sudo -iu "$CURRENT_USER" \
find "$SEARCH_PATH" -type f -size +${SIZE_LIMIT}k -print
Delete Large Files
Find and delete files larger than specified size:
#!/bin/bash
# Get current user context
CURRENT_USER=$(stat -f "%Su" /dev/console)
CURRENT_USER_UID=$(id -u "$CURRENT_USER")
# Configuration
SEARCH_PATH="/Users/$CURRENT_USER/Downloads"
SIZE_LIMIT="50" # Size in kilobytes
echo "⚠️ WARNING: This will permanently delete files larger than ${SIZE_LIMIT}KB"
echo "Searching in: $SEARCH_PATH"
# List and delete large files
launchctl asuser $CURRENT_USER_UID sudo -iu "$CURRENT_USER" \
find "$SEARCH_PATH" -type f -size +${SIZE_LIMIT}k -print -delete
echo "✅ Large files deleted successfully"
Enhanced File Cleanup Script
Script with safety checks and detailed reporting:
#!/bin/bash
# Function to cleanup large files safely
cleanup_large_files() {
local search_path="$1"
local size_limit="$2"
local dry_run="${3:-false}"
# Get current user context
local current_user=$(stat -f "%Su" /dev/console)
local current_user_uid=$(id -u "$current_user")
if [[ ! -d "$search_path" ]]; then
echo "❌ Error: Directory not found: $search_path"
return 1
fi
echo "=== Large Files Cleanup ==="
echo "📁 Path: $search_path"
echo "📏 Size limit: ${size_limit}KB"
echo "👤 User: $current_user"
echo "🔍 Dry run: $dry_run"
echo "=========================="
# Find large files
local files_command="find '$search_path' -type f -size +${size_limit}k"
if [[ "$dry_run" == "true" ]]; then
echo "📋 Files that would be deleted:"
launchctl asuser $current_user_uid sudo -iu "$current_user" \
bash -c "$files_command -ls"
local count=$(launchctl asuser $current_user_uid sudo -iu "$current_user" \
bash -c "$files_command | wc -l")
echo "📊 Total files found: $count"
else
echo "🗑️ Deleting large files..."
launchctl asuser $current_user_uid sudo -iu "$current_user" \
bash -c "$files_command -print -delete"
echo "✅ Cleanup completed"
fi
}
# Configuration
SEARCH_PATH="/Users/$(stat -f "%Su" /dev/console)/Downloads"
SIZE_LIMIT="100"
DRY_RUN="true" # Set to "false" to actually delete files
# Run cleanup
cleanup_large_files "$SEARCH_PATH" "$SIZE_LIMIT" "$DRY_RUN"
Enterprise Storage Management
Comprehensive storage cleanup for enterprise environments:
#!/bin/bash
# Enterprise storage cleanup configuration
CLEANUP_LOCATIONS=(
"/Users/*/Downloads:50"
"/Users/*/Desktop:100"
"/Users/*/Documents:200"
"/tmp:10"
)
# Function to format file size
format_size() {
local size=$1
if [[ $size -gt 1048576 ]]; then
echo "$(( size / 1048576 ))GB"
elif [[ $size -gt 1024 ]]; then
echo "$(( size / 1024 ))MB"
else
echo "${size}KB"
fi
}
# Function to get directory size
get_directory_size() {
local dir="$1"
if [[ -d "$dir" ]]; then
du -sk "$dir" 2>/dev/null | cut -f1
else
echo "0"
fi
}
# Enterprise cleanup execution
echo "🏢 MacFleet Enterprise Storage Cleanup"
echo "======================================"
echo "Generated: $(date)"
echo "Device: $(hostname)"
total_freed=0
current_user=$(stat -f "%Su" /dev/console)
current_user_uid=$(id -u "$current_user")
for location_config in "${CLEANUP_LOCATIONS[@]}"; do
IFS=':' read -r location_pattern size_limit <<< "$location_config"
echo ""
echo "📁 Processing: $location_pattern (>${size_limit}KB)"
# Expand pattern for user directories
for location in $location_pattern; do
if [[ -d "$location" ]]; then
echo " 🔍 Scanning: $location"
# Get before size
before_size=$(get_directory_size "$location")
# Find and delete large files
deleted_files=$(launchctl asuser $current_user_uid sudo -iu "$current_user" \
find "$location" -type f -size +${size_limit}k -print -delete 2>/dev/null | wc -l)
# Get after size
after_size=$(get_directory_size "$location")
freed=$((before_size - after_size))
total_freed=$((total_freed + freed))
if [[ $deleted_files -gt 0 ]]; then
echo " ✅ Deleted: $deleted_files files"
echo " 💾 Freed: $(format_size $freed)"
else
echo " ℹ️ No large files found"
fi
fi
done
done
echo ""
echo "======================================"
echo "📊 Summary:"
echo " Total space freed: $(format_size $total_freed)"
echo " Cleanup completed: $(date)"
if [[ $total_freed -gt 0 ]]; then
echo "✅ Storage cleanup successful"
else
echo "ℹ️ No cleanup needed - storage usage is optimal"
fi
Safe Cleanup with Backup Option
Script with optional backup before deletion:
#!/bin/bash
# Function to backup large files before deletion
backup_and_delete() {
local search_path="$1"
local size_limit="$2"
local backup_enabled="${3:-false}"
local current_user=$(stat -f "%Su" /dev/console)
local current_user_uid=$(id -u "$current_user")
local backup_dir="/tmp/macfleet_backup_$(date +%Y%m%d_%H%M%S)"
echo "🔄 Processing: $search_path"
if [[ "$backup_enabled" == "true" ]]; then
echo "📦 Creating backup directory: $backup_dir"
mkdir -p "$backup_dir"
# Find and backup large files
launchctl asuser $current_user_uid sudo -iu "$current_user" \
find "$search_path" -type f -size +${size_limit}k -exec cp {} "$backup_dir/" \;
echo "✅ Files backed up to: $backup_dir"
fi
# Delete large files
local deleted_count=$(launchctl asuser $current_user_uid sudo -iu "$current_user" \
find "$search_path" -type f -size +${size_limit}k -print -delete | wc -l)
echo "🗑️ Deleted $deleted_count large files"
if [[ "$backup_enabled" == "true" && $deleted_count -gt 0 ]]; then
echo "💡 Backup location: $backup_dir"
echo "⏰ Backup will be auto-removed in 7 days"
# Schedule backup cleanup (requires launchd or cron)
echo "find '$backup_dir' -type f -mtime +7 -delete" > /tmp/cleanup_backup.sh
chmod +x /tmp/cleanup_backup.sh
fi
}
# Configuration
SEARCH_PATH="/Users/$(stat -f "%Su" /dev/console)/Downloads"
SIZE_LIMIT="100"
BACKUP_ENABLED="true"
# Execute safe cleanup
backup_and_delete "$SEARCH_PATH" "$SIZE_LIMIT" "$BACKUP_ENABLED"
Usage with MacFleet
- Configure search path and size limit in the script
- Choose between dry-run and actual deletion modes
- Consider enabling backup option for safety
- Deploy through MacFleet's remote script execution
- Monitor results and freed space in action history
Size Units and Examples
Unit | Description | Example Use Case |
---|---|---|
+50k | Files larger than 50KB | Small document cleanup |
+10M | Files larger than 10MB | Media file cleanup |
+100M | Files larger than 100MB | Large file cleanup |
+1G | Files larger than 1GB | Archive cleanup |
Common Cleanup Targets
Directory | Typical Size Limit | Purpose |
---|---|---|
Downloads | 50MB | Remove large downloads |
Desktop | 100MB | Clean desktop files |
Documents | 200MB | Archive old documents |
Temp folders | 10MB | System cleanup |
Safety Considerations
⚠️ CRITICAL WARNING: Files deleted with these scripts are permanently removed and cannot be recovered from Trash.
- Test first: Always run with dry-run mode enabled initially
- Backup important data: Consider backup option for valuable files
- Verify paths: Double-check search directories before execution
- User context: Scripts run in current user context for security
Troubleshooting
Permission denied: Ensure proper user context with launchctl asuser
Directory not found: Verify search paths exist before running
No files found: Check size limits and directory contents
Note: Always validate scripts on test systems before bulk deployment. Permanent file deletion requires careful consideration and backup planning.