How to Bulk Delete lscache Folders in WHM/cPanel Accounts
Introduction:
The Impact of lscache on Server Storage: When using LiteSpeed Web Server with the LSCache plugin (for WordPress, Magento, etc.), the server generates static cache files to boost performance. These files are typically stored in a folder named lscache within the user’s home directory (/home/username/lscache). So, lets learn How to Bulk Delete lscache Folders in WHM/cPanel Accounts?
While caching is essential for speed, these folders can grow exponentially, consuming GBs of disk space and millions of inodes. If you are performing server migrations, troubleshooting caching issues, or simply running low on storage, bulk-deleting these folders is the most efficient solution.
The Efficiency of This Script
Unlike manual deletion, this script:
-
Loops through all accounts: Automatically identifies every user on the system.
-
Verifies Existence: It checks if the
lscachedirectory exists before attempting a delete command, preventing errors. -
Complete Removal: It removes the entire directory to ensure a fresh cache start for all websites.
The Automated lscache Cleanup Script
Follow these steps to perform the cleanup as the root user.
Step 1: Access Your Server
Connect to your server via SSH as the root user.
Step 2: Create the Cleanup Tool
Create a new script file in your root directory:
nano /root/clean_lscache.sh
Step 3: Paste the Script Code
Copy 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/lscache" ]; then
rm -rf "$HOMEDIR/lscache"
echo "Deleted lscache folder for: $user"
fi
done
Step 4: Save and Permissions
-
Press Ctrl+O and Enter to save.
-
Press Ctrl+X to exit the editor.
-
Set the execution permission:
chmod +x /root/clean_lscache.sh
Step 5: Run the Script
Execute the script to start the bulk deletion:
/bin/bash /root/clean_lscache.sh
Technical Breakdown: How it Works
-
awk '{print $2}' /etc/trueuserdomains: This precisely targets the list of usernames managed by WHM. -
getent passwd "$user" | cut -d: -f6: This is the safest way to find a user’s home directory. It works even if your accounts are spread across different partitions (e.g.,/home,/home2). -
if [ -d "$HOMEDIR/lscache" ]: This safety check ensures the script only attempts to delete existing directories. -
rm -rf "$HOMEDIR/lscache": This performs a recursive and forceful deletion of the folder. Note: LiteSpeed will automatically recreate this folder as soon as a new visitor hits a cached page, so this is safe to use.
Conclusion
Bulk deleting the lscache folder is a highly effective maintenance task for LiteSpeed-powered servers. It helps in maintaining lean backups and preventing inode exhaustion. After running this script, you will likely notice a significant drop in disk usage across your WHM environment.
Experience the ultimate speed of LiteSpeed on our Managed VPS Hosting India or deploy high-availability apps on our Cloud Hosting with myglobalHOST.


