Tutorial

New updates and improvements to Roundfleet.

Important notice

The code examples and scripts provided in these tutorials are for educational purposes only. Roundfleet is not responsible for any issues, damages, or security vulnerabilities that may arise from the use, modification, or implementation of these examples. Always review and test code in a secure environment before using it in production systems.

Empty Trash on macOS

Learn how to remotely empty trash folders on Mac devices using scripts. This helps free up disk space and manage storage efficiently across your Mac fleet.

Empty Trash for Specific User

Remove all items from a specific user's trash:

#!/bin/bash

# Specify the username
USERNAME="john"

# Empty trash for specific user
sudo rm -rf /Users/"${USERNAME}"/.Trash/*

echo "Trash emptied for user: ${USERNAME}"

Empty Trash for Current User

Automatically detect and empty trash for the currently logged-in user:

#!/bin/bash

# Get current user
CURRENT_USER=$(stat -f "%Su" /dev/console)

# Empty trash for current user
su "$CURRENT_USER" -c "rm -rf ~/.Trash/*"

echo "Trash emptied for current user: ${CURRENT_USER}"

Enhanced Script with Validation

Script with error handling and logging:

#!/bin/bash

# Function to log messages
log_message() {
    echo "$(date '+%Y-%m-%d %H:%M:%S'): $1"
}

# Get current user
CURRENT_USER=$(stat -f "%Su" /dev/console)

if [[ -z "$CURRENT_USER" ]]; then
    log_message "Error: Could not determine current user"
    exit 1
fi

log_message "Emptying trash for user: ${CURRENT_USER}"

# Check if trash directory exists
TRASH_DIR="/Users/${CURRENT_USER}/.Trash"
if [[ ! -d "$TRASH_DIR" ]]; then
    log_message "Trash directory not found: ${TRASH_DIR}"
    exit 1
fi

# Count items before deletion
ITEM_COUNT=$(find "$TRASH_DIR" -mindepth 1 -maxdepth 1 | wc -l)
log_message "Found ${ITEM_COUNT} items in trash"

if [[ $ITEM_COUNT -eq 0 ]]; then
    log_message "Trash is already empty"
    exit 0
fi

# Empty trash
if su "$CURRENT_USER" -c "rm -rf ~/.Trash/*"; then
    log_message "Successfully emptied trash for ${CURRENT_USER}"
else
    log_message "Failed to empty trash"
    exit 1
fi

Multi-User Trash Management

Empty trash for all users on the system:

#!/bin/bash

# Function to log messages
log_message() {
    echo "$(date '+%Y-%m-%d %H:%M:%S'): $1"
}

log_message "Starting multi-user trash cleanup"

# Find all user directories
for USER_DIR in /Users/*; do
    if [[ -d "$USER_DIR" && ! "$USER_DIR" =~ /Users/(Shared|Guest) ]]; then
        USERNAME=$(basename "$USER_DIR")
        TRASH_DIR="$USER_DIR/.Trash"
        
        if [[ -d "$TRASH_DIR" ]]; then
            ITEM_COUNT=$(find "$TRASH_DIR" -mindepth 1 -maxdepth 1 2>/dev/null | wc -l)
            
            if [[ $ITEM_COUNT -gt 0 ]]; then
                log_message "Emptying trash for user: ${USERNAME} (${ITEM_COUNT} items)"
                sudo rm -rf "$TRASH_DIR"/*
                log_message "Completed trash cleanup for: ${USERNAME}"
            else
                log_message "Trash already empty for user: ${USERNAME}"
            fi
        fi
    fi
done

log_message "Multi-user trash cleanup completed"

Usage with Roundfleet

  1. Choose the appropriate script based on your needs
  2. For specific users, modify the USERNAME variable
  3. Deploy through Roundfleet's remote script execution
  4. Monitor results in the action history

Prerequisites

  • Full Disk Access: Grant full disk access to Roundfleet agent in System Settings > Privacy & Security > Full Disk Access
  • Administrative privileges: Scripts require sudo access for system-wide operations

Security Considerations

  • Verify user permissions before running scripts
  • Consider backing up important files before mass trash deletion
  • Test on limited devices before fleet-wide deployment

Troubleshooting

Permission denied: Ensure Roundfleet agent has Full Disk Access User not found: Verify username exists on the target device Script fails: Check if user is currently logged in for user-specific operations


Note: Deleted items cannot be recovered after running these scripts. Always validate on test systems first.

Related Tutorials

Storage Management on macOS

Monitor disk usage, implement storage policies, and optimize storage performance across macOS devices using advanced storage management, automated cleanup, and enterprise analytics for organizational storage infrastructure.

Jul 7, 2025

Hide and Unhide Files and Folders on macOS

Control file and folder visibility on Mac devices using chflags commands and Finder settings for enhanced security and organization.

Jul 7, 2025

File Size and Storage Analysis on macOS

Learn how to analyze file and folder sizes, monitor disk usage, identify storage hogs, and implement comprehensive storage management on macOS devices using command-line tools for effective storage optimization.

Jul 7, 2025

File Protection on macOS

Implement comprehensive file and application protection on macOS devices using immutable flags, access controls, and enterprise security policies for organizational data protection.

Jul 7, 2025

File Ownership Management on macOS

Learn how to change file and folder ownership, manage permissions, and implement comprehensive ownership policies on macOS devices using command-line scripts for secure file system management.

Jul 7, 2025

File and Folder Open Management on macOS

Comprehensive guide for enterprise file and folder opening operations on Roundfleet devices with security validation, application control, and automated access management.

Jul 7, 2025

Wi-Fi Network Management on macOS

Manage Wi-Fi networks, configure proxy settings, DNS servers, and implement enterprise network policies on macOS devices using advanced networksetup commands and centralized fleet management.

Jul 7, 2025

Wi-Fi Control on macOS

Comprehensive guide to controlling Wi-Fi settings on macOS devices using networksetup commands for enterprise wireless management, security policies, and power optimization across fleet deployments.

Jul 7, 2025