Join GitHub today
File cache table excessively large (and does not shrink after data removal) / Nextcloud should defragment file cache table #7312
My nextcloud instance (small-scale, single-server setup, 6 users) has an excessively large database size (20 GB as of today). This size doesn't sensibly relate to the amount of data managed (approx. 1 TB, now reduced to 4.3 GB).
I have found that almost all of the 20GB is located in a single table, the file cache:
This file seems to grow and grow, but never shrinks.
Here’s something I don’t understand: there should be thousands of orphaned entries now that most of the data is gone!
=> I suspect that my file cache table is somehow broken, but I don’t know how to fix it. I believe I should clear the table and reproduce it, but I do not know how to do that.
Steps to reproduce
Flie cache table should shrink after external storage was removed.
File cache table stays the same (and is excessively large)
General server configuration
Operating system: Linux helge 4.13.0-17-generic #20-Ubuntu SMP Mon Nov 6 10:04:08 UTC 2017 x86_64
Web server: Apache/2.4.28 (Unix) OpenSSL/1.0.2g (fpm-fcgi)
Database: mysql 5.7.18
PHP version: 7.0.23
Nextcloud version: 11.0.5 (stable) - 220.127.116.11 (snap version 3680)
Updated from an older Nextcloud/ownCloud or fresh install: Nextcloud snap, updated from previous snap versions (3317, 2707)
Where did you install Nextcloud from: snap
Are you using external storage, if yes which one: \OC\Files\Storage\Local, see description for details!
Are you using encryption: no
Are you using an external user-backend, if yes which one: Nextcloud sync client from the ubuntu store
Content of config/config.php
Browser: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/605.1 (KHTML, like Gecko) Version/11.0 Safari/605.1 Ubuntu/17.10 (3.26.1-1ubuntu1) Epiphany/3.26.1 (Web App)
Operating system: GNU/Linux (Ubuntu 17.10)
Yes, there is no way with NC commands to reduce oc_filecache table if files&folders are deleted or renamed outside NC. It seems the same apply to external shares. If changes are made outside, NC can only add files to oc_filecache.
I my case oc_filecache was expanded due large amount of photos and previews generated by previewgenerator app (20 previews for each photo generated by default). Every preview is also registered in oc_filecache table. I had to modify previewgenerator, manually delete all previews and all db records for files that referenced to preview folder. Then regenerate reduced amount of previews again. It's time consuming process.
There is definitely need for NC maintenance command which keep oc_filecache table optimized according to local filesystem. It would also allows more easily to use NC for fastlane file upload and managing with direct Samba/FTP shares. Because http/webdav upload for numerous files is lagging and not as robust and safe as Samba/FTP.
@ruedigerkupper : I can't say. There are several other records regarding NC system in that table, not only files. So I wouldn't do it. I saw a thread where someone presented php file which make comparison of actual files and records in database and delete db records for the non-existent. But nobody confirms it's working and my knowledge of MySQL is too short to make conclusion.
I didn't risk. Because most overhead was due experiments with previews I'd managed only them. I put NC in maintenance mode and made backup of database. Delete all content of appdata_.../preview/ folder. With phpmyadmin selected (with search) all records in oc_filecache table where column "path" contain appdata_your-NCspecificpath-here/preview/ and deleted them. I have NC12. I don't know maybe NC11 put previews in different folder (under each user).
Then I modified previewgenerator - rullzer/previewgenerator#78
Switch maintenance mode off and run preview:generate-all. In my case it takes almost 3 days to finish generating them for ~85 K photos on i3 CPU.
At this moment I have problem with this - #7269
Thanks, ArnisR! It have meanwhile solved my problem, and it turned out too be much easier than that:
Note "Data_length" (=21 MB) and "Data_free" (=19 GB)!! So, the removed files had actually been cleaned from the file cache, but the InnoDB table was so badly fragmented that it occupied 1000 times the disk space needed for its data.
Going through the mysql reference, I came about https://dev.mysql.com/doc/refman/5.7/en/innodb-file-defragmenting.html. That states that an InnoDB table can be defragmented by issuing a "no-op" ALTER TABLE command:
And -- magically -- this reduced the table to a file size of 8 MB (!!!):
So let's get this straight:
Nextcloud should defragment the oc_filecache table regularly, probably as part of the regular maintenance cron job.
changed the title
File cache table excessively large (and does not shrink after data removal)
Nov 28, 2017
Same issue here, our internal next cloud service has only a few hundred users, but the database was over 130GB. We just upgraded from owncloud 9 to nextcloud 12.0.4 and manually cleared the oc_filecache table leaving only the shared links ( with the php script ArnisR mentioned), this leaves only about 400MB, with the scan for all local directories, the database size is only at 700MB(only 2TB data locally), and the web interface functions normally (the service will rescan mounted directories when a user enters that mounted directory, and display the files).
With a closer look at the file system, we found out of the space database inserts are from different users mounting the same file systems over smb (the fs has a few PB of data). The bit of annoying thing is when doing the occ files:scan $user command, the service will actually try to scan all the mounted folders this user have, including the smb mounted folder, which takes forever, we couldn't afford to have users waiting for those scans, so wrote a script to scan only local files.
So here are a few suggestions:
@ArnisR, could you give an idea on how long the defragmenting took for you?
For Info, I could get the oc_filecache.ibd growing rate from recent backups:
referenced this issue
Jan 10, 2019
MySQL regulary stops working because of this:
I'm quite surprised that there is no "clean up files in index that have actually been deleted on disk" functionality in Nextcloud, since this is obviously vital to keep Nextcloud running smoothly.
I wonder, we also have SMB mounted external storages and for those Nextcloud automatically cleans up the filecache for files that get deleted (and I did not activate the notify feature as far as I'm aware) ... why does this not work for local files?