Overview
This document provides step-by-step instructions on how to clean up deleted files (trash) for all users in OwnCloud using the occ command. It also includes methods to list users, check storage usage, and automate trash cleanup with a cron job.
1️⃣ Fix Permissions to Run OCC Command
OwnCloud’s occ command must be executed as the user that owns config.php. If you run it as root, you’ll see an error like:
Console has to be executed with the user that owns the file config/config.php
Solution: Switch to the Correct User
Find the correct user with:
ls -l /home/mwsqcloudin/public_html/cb/config/config.php
If it shows mwsqcloudin, switch to that user:
su - mwsqcloudin
Then navigate to the OwnCloud directory:
cd /home/mwsqcloudin/public_html/cb/
Now you can use php occ commands.
2️⃣ List All Users in OwnCloud
To display a list of all users:
php occ user:list
Fix for Incorrectly Formatted Output
If usernames appear with - username:, extract only the actual username:
php occ user:list | awk -F ': ' '{print $2}'
3️⃣ Check Storage Usage for Each User
OwnCloud does not support php occ user:info <username>, so we used database queries and file system commands.
Option 1: MySQL Query to Get Storage Usage
- Access MySQL:
mysql -u root -p - Use the OwnCloud database:
USE owncloud_db; - Run the query:
SELECT a.user_id AS 'Username', SUM(f.size) / 1024 / 1024 AS 'Used Space (MB)' FROM oc_storages s JOIN oc_filecache f ON s.numeric_id = f.storage LEFT JOIN oc_accounts a ON s.numeric_id = a.id GROUP BY a.user_id ORDER BY `Used Space (MB)` DESC;
Option 2: Check Storage from File System
To check actual disk usage per user:
du -sh /home/mwsqcloudin/public_html/cb/data/*
4️⃣ Remove All Trash Files for Each User
Attempt 1 (Did Not Work)
php occ trashbin:cleanup --all-users
❌ This failed because --all-users is not available in our OwnCloud version.
Final Working Solution
To remove trash for all users:
php occ user:list | awk -F ': ' '{print $2}' | while read user; do
echo "Cleaning trash for user: $user"
php occ trashbin:cleanup "$user"
done
✅ This iterates through all users and removes their trash.
If OCC Cleanup Fails: Manually Delete Files
Use this command to delete all files older than 30 days from trash:
find /home/mwsqcloudin/public_html/cb/data/*/files_trashbin/ -type f -mtime +30 -delete
5️⃣ Automate Trash Cleanup with a Cron Job
To schedule daily trash cleanup, add a cron job:
Step 1: Open Crontab
crontab -e
Step 2: Add the Following Line
0 3 * * * cd /home/mwsqcloudin/public_html/cb/ && php occ user:list | awk -F ': ' '{print $2}' | while read user; do php occ trashbin:cleanup "$user"; done
✅ This will automatically remove trashed files for all users at 3 AM daily.
✅ Summary of Steps
| Step | Command |
|---|---|
| Fix permissions | su - mwsqcloudin |
| Navigate to OwnCloud directory | cd /home/mwsqcloudin/public_html/cb/ |
| List users | php occ user:list |
| Extract only usernames | `php occ user:list |
| Check storage (MySQL query) | Run the provided SQL query in MySQL |
| Check storage (Linux command) | du -sh /home/mwsqcloudin/public_html/cb/data/* |
| Remove trash for a single user | php occ trashbin:cleanup "username" |
| Remove trash for all users | Run the looped occ trashbin:cleanup command |
| Delete trash files manually | find /home/mwsqcloudin/public_html/cb/data/*/files_trashbin/ -type f -mtime +30 -delete |
| Automate cleanup with cron | Add the cron job in crontab -e |
🚀 Now, Your OwnCloud is Fully Optimized!
- ✅ You can now manage user storage effectively.
- ✅ Deleted files are cleaned automatically.
- ✅ Storage reports can be generated from MySQL.
Let me know if you need any modifications! 😊🚀
