File is locked - how to unlock

Is a sudo -u www-data php occ files:scan – do also check for file if i’m using Object Storage as primary way to keep files?

I tried the above steps, didn’t work for me.

Also the table got some entries with lock state -1 or 2. What do those mean? Do I have to delete them aswell?

For those that do not know how to connect or even the owncloud database name in mysql (mariadb) , follow these instructions :

0 - put your nextcloud under maintenance mode on config.php , so no one access it while you are doing changes .

1st - open your config.php in your nextcloud server folder under “config” folder .

2nd - there should be a field the like this : " ‘dbname’ => " , in this field there is your database name .

3rd - open a terminal window and execute under a root account :

  • mysql -u root -p
    "enter your password"

then connect to nextcloud database with the following command :

  • connect database
    database = your config.php databasename field

after this point all you have to do is to issue the sql command to delete all data inside the oc_file_locks table

-DELETE FROM oc_file_locks WHERE 1;

then after this point all you have to do is :

to close sql prompt and you are done .

Remove the maintenance mode from config.php and start uploading your files over web interface or dav .


I have similar troubles with a Nextcloud installation that I migrated from an Owncloud installation that I’m running on a Synology diskstation.

This is the database command that I tried:
DELETE FROM owncloud.file_locks WHERE file_locks.lock=1;
but this did not work, so later on I tried:
sudo -u http php56 occ files:scan --all
(note that I had to use the ‘http’ for username instead of ‘www-data’ and that I had to explicitely state the version in the php56 command.)

The scan resulted also in exceptions while scanning:
Exception while scanning: "files/0e3e13890e7b79a0edc572f53b262079" is locked #0 /volume1/web/nextcloud/lib/private/Files/Storage/Common.php(680): OC\Lock\DBLockingProvider->acquireLock('files/0e3e13890...', 1) #1 /volume1/web/nextcloud/lib/private/Files/Storage/Wrapper/Wrapper.php(588): OC\Files\Storage\Common->acquireLock('files/Familie/B...', 1, Object(OC\Lock\DBLockingProvider)) #2 /volume1/web/nextcloud/lib/private/Files/Storage/Wrapper/Wrapper.php(588): OC\Files\Storage\Wrapper\Wrapper->acquireLock('files/Familie/B...', 1, Object(OC\Lock\DBLockingProvider)) #3 /volume1/web/nextcloud/lib/private/Files/Cache/Scanner.php(148): OC\Files\Storage\Wrapper\Wrapper->acquireLock('files/Familie/B...', 1, Object(OC\Lock\DBLockingProvider)) #4 /volume1/web/nextcloud/lib/private/Files/Cache/Scanner.php(420): OC\Files\Cache\Scanner->scanFile('files/Familie/B...', 3, '157625', NULL, true) #5 /volume1/web/nextcloud/lib/private/Files/Cache/Scanner.php(388): OC\Files\Cache\Scanner->handleChildren('files/Familie', true, 3, '157625', true, 53184942) #6 /volume1/web/nextcloud/lib/private/Files/Cache/Scanner.php(391): OC\Files\Cache\Scanner->scanChildren('files/Familie', true, 3, 157625, true) #7 /volume1/web/nextcloud/lib/private/Files/Cache/Scanner.php(391): OC\Files\Cache\Scanner->scanChildren('files', true, 3, 23, true) #8 /volume1/web/nextcloud/lib/private/Files/Cache/Scanner.php(322): OC\Files\Cache\Scanner->scanChildren('', true, 3, 21, true) #9 /volume1/web/nextcloud/lib/private/Files/Utils/Scanner.php(196): OC\Files\Cache\Scanner->scan('', true, 3) #10 /volume1/web/nextcloud/apps/files/lib/Command/Scan.php(159): OC\Files\Utils\Scanner->scan('/jan') #11 /volume1/web/nextcloud/apps/files/lib/Command/Scan.php(227): OCA\Files\Command\Scan->scanFiles('jan', '/jan', false, Object(Symfony\Component\Console\Output\ConsoleOutput), false) #12 /volume1/web/nextcloud/3rdparty/symfony/console/Command/Command.php(256): OCA\Files\Command\Scan->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #13 /volume1/web/nextcloud/core/Command/Base.php(161): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #14 /volume1/web/nextcloud/3rdparty/symfony/console/Application.php(818): OC\Core\Command\Base->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #15 /volume1/web/nextcloud/3rdparty/symfony/console/Application.php(186): Symfony\Component\Console\Application->doRunCommand(Object(OCA\Files\Command\Scan), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #16 /volume1/web/nextcloud/3rdparty/symfony/console/Application.php(117): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #17 /volume1/web/nextcloud/lib/private/Console/Application.php(169): Symfony\Component\Console\Application->run(NULL, NULL) #18 /volume1/web/nextcloud/console.php(90): OC\Console\Application->run() #19 /volume1/web/nextcloud/occ(11): require_once('/volume1/web/ne...') #20 {main}

My Nextcloud client is still giving errors like:
Error transferring - server replied: Locked

Do you have any suggestions?

I did some further investigations and noted that the owncloud.file_locks table had some more rows where the lock was <> 0, but not 1:
SELECT * FROM owncloud.file_locks WHERE file_locks.lock<>0;
which resulted in:
| id | lock | key | ttl |
+-------+------+----------------------------------------+------------+ | 14 | -1 | files/0e3e13890e7b79a0edc572f53b262079 | 1459283811 | | 15 | -1 | files/4f8cbf29cadcc31429f83d792e806885 | 1459283811 | | 89 | 26 | files/e96e1dcb01f6d44a74247b4ec38f0170 | 1496838714 | | 22846 | 27 | files/46e069d0509232e543a2112007e46765 | 1496838719 | | 22848 | 26 | files/bc4e386d83896b627f63b4e7f48c2d1e | 1496838719 | | 23877 | 25 | files/27de4bb5c6042f2a920e605c9884fdcd | 1496840605 | +-------+------+----------------------------------------+------------+

I deleted the rows where the lock value is -1
DELETE FROM owncloud.file_locks WHERE id=14;
DELETE FROM owncloud.file_locks WHERE id=15;

This solved the problem that I was encountering.

Note that there are also files with a lock value of 25, 26, and 27. I did not research the meaning of these values yet, nor the meaning of -1.


Some remarks to the initial post at the top of this thread:

  1. The SQL statement overthere is not completely correct.
    a. In my Nextcloud the table was just named file_locks
    b. The where clause should be something like: WHERE file_locks.lock=1
  2. The SQL command I used is:
    DELETE FROM owncloud.file_locks WHERE file_locks.lock=1;
  3. There can be other lock values then 0 and 1 as well, like -1, 25, 26, and 27. I had to delete the ones with value -1 as well.

See my above posts for more details.

'maintenance' => true,
'filelocking.enabled' => false,

These two items helped me fix the problem.

  1. I put Nextcloud in maintenance mode
  2. Used Adminer to access the file_locs table.
  3. The “lock” field in most rows was zero. In others it was 17, 11, or 4. I changed all these values to zero.
  4. Went to the Web interface, and deleted the stubborn items in question.
  5. Went back to Adminer, and reset the values to what they were previously.
  6. Confirmed operation by creating then deleting the same name files and folders using identical paths.

I am on a shared server, so turned off file locking, and am monitoring the situation.

Thanks for the great info.



I’m running into the locking problem and found this text. It is a very good guidance. It hould be part of the authoritive answer!

But the text of the last 2 bullets of the permanent solution seem to contradict.
Bullet 2 states "You can disable the file locking,"
Bullet 3 states "Disabling is no solution"
Is the last bullet a comment on the 2nd? Or are they talking about 2 different "disablings"
Can someone edit it to solve this once and for all?



I had the problem with locked files too. In my case I found that I have cron job configured to start through AJAX. Problem was, that I rarely used web interface, so after I run cron.php manually in Nextcloud install root, everything went to normal. I also configured it to run as a cron job every 15 minutes. See manual how to configure Nextcloud cron jobs.

what are the reasons of file locked ? Is it normal ?


I had this issue when I tried to delete a file, but it resulted in an Apache segmentation fault. I fixed this bug and tried to delete the file again but this time it was locked.

So I think the issue is that Nextcloud locks the file before trying delete it and then, if there’s a crash or other error, the lock is never released.

I think the UI should make it clearer what file is locked and provide some way to unlock it. Or, better, have some mechanism to prevent files from being locked and never unlocked afterwards.

1 Like

If this is your own server then you should set up Redis like @tflidd recommends.

Before following this suggestions, I would like to know…

For what purpose file locking was created ?

I have 40.000 entries in my oc_file_locs table, that seems a little too much for me!
Also wiping out all of this locks, seems a bit dangerous to me just to fix 1 problem (I can’t delete a folder…)

1 Like

As far as i know its there, so that you no other process can access the file while it is written, or otherwise processed.

The issue is, that there is maybe a logical inconsistency, since i could not find an “1” entries under the lock column. If you delete all entries, they will be rebuild automatically when you exit maintenance mode, but this time in a correct order. I tried this on a testsystem, before applying this on our production mashine, and it worked just fine.

Keep in mind, i drew some assoumtions here. I would be happy if someone could either confirm this, or correct me if nessecary.

Thank you for your answer.

I would also like to have some confirmation… like… is it normal to have 40.000 locks in a database?
Is my locking system working good? I have around 100.000 files.

I also have 5 main directories owned by a “admin” owner and shared with the others, so everyone uploads files in that directories.
I made this choice so that the “admin” user still owns the files if someone go rogue.

Is this the reason why I have 50% of files locked ?

If you have that many locks in your database you have a serious problem. Use Redis for transactional file locking as said in the first post by @tflidd.

If they are stored in your database you don’t use transactional locking as described here: : Transactional file locking — Nextcloud 13 Administration Manual 13 documentation

I get the same error message when doing a “occ files:scan” for a specific user. The error starts with:

Exception during scan: File entry could not be inserted with insertIfNotExist() but could also not be selected with getId() in order to perform an update. Please try again.

Then 21 lines with more specific errors. What´s wrong here? How can I perform a “file system vs. database nc_filecache” comparison?

I get that same error too for one of my users, tried to do occ files:cleanup but it did not help (0 orphaned file cache entries deleted)
How do we go about this issue?

Instructions is in the first post :wink:

Yes of course, and I already did manually disable the locking state beforehand. I had this a while ago already, that’s where I started using redis, but the problem seems to persist
MariaDB says: Query OK, 0 rows affected (0.01 sec)
fsck.ext4 says that everything is fine