Print

How to Bulk Delete /tmp Folder in WHM/cPanel Accounts

Introduction

Why Delete /tmp Folder in cPanel and is it safe? Every cPanel account has a private /tmp folder located within its home directory (e.g., /home/username/tmp). This folder stores session files, temporary upload data, and cached logs. Over time, these files can accumulate into thousands of small inodes, consuming significant disk space and potentially slowing down backup processes. Let’s learn How to Bulk Delete /tmp Folder in WHM/cPanel Accounts?

Manually deleting these files for hundreds of accounts is impossible. This guide introduces a powerful Bash script to automate the process safely.

The Script Logic

The script we are using performs three critical actions:

  1. Identifies Users: It pulls a list of all active cPanel users from /etc/trueuserdomains.

  2. Locates Home Directories: It dynamically finds the correct home path for each user.

  3. Cleans Safely: It deletes the contents of the /tmp folder but keeps the directory structure intact to avoid breaking application functionality.


The Automated Cleanup Script

Follow these steps to execute the bulk cleanup on your server.

Step 1: Access Your Server via SSH

Log in to your server as the root user using an SSH client like PuTTY or Terminal.

Step 2: Create the Cleanup Script

Create a new file to house the script:

nano /root/clear_tmp.sh

Step 3: Paste the Code

Copy and paste the following code exactly into the editor:

for user in $(awk '{print $2}' /etc/trueuserdomains); do
    HOMEDIR=$(getent passwd "$user" | cut -d: -f6)
    if [ -d "$HOMEDIR/tmp" ]; then
        rm -rf "$HOMEDIR/tmp"/*
        echo "Emptied tmp for: $user"
    fi
done

Step 4: Save and Set Permissions

  • Press Ctrl+O and Enter to save, then Ctrl+X to exit.

  • Make the script executable by running:

chmod +x /root/clear_tmp.sh

Step 5: Execute the Script

Run the script to begin the bulk deletion process:

/bin/bash /root/clear_tmp.sh

Breakdown of the Command

  • awk '{print $2}' /etc/trueuserdomains: This part accurately fetches the list of usernames on the server, ignoring the domains.

  • getent passwd "$user": This ensures that even if you have users on different mount points (like /home2 or /home3), the script will find them correctly.

  • rm -rf "$HOMEDIR/tmp"/*: The asterisk (*) at the end is crucial—it deletes the files inside the folder while leaving the /tmp directory itself untouched, preventing errors in PHP or cPanel.

Conclusion

Regularly clearing the /tmp folders of your cPanel accounts is a best practice for server health, especially before running full server backups. This script provides a transparent and efficient way to maintain your server without the overhead of third-party plugins.

Take full control of your server performance with Managed VPS Hosting India and high-speed Cloud Hosting solutions from myglobalHOST—where security and speed are built-in.

Table of Contents
Close
Get 75% + extra 10% Discount on web Hosting Plans by myglobalHOST

Extra 10% OFF

Coupon Code

EXTRA10

Applicable on

FLAT Rs 100 OFF

Coupon Code

FLAT100

Applicable on

How to Avail: Simply browse the most appropriate hosting plan for you and avail extra discount on all orders.

Expert Advice

+91-79862-84663

11AM to 6PM

Live Chat : 11AM to 6PM