Short tip: Remove Nextcloud File Locks

To prevent accidental overwriting of files during synchronization - especially between multiple users and clients - Nextcloud relies on File Locking. This function is implemented at application level and therefore has no special requirements for the file system used.

However, with large amounts of data and network problems, a dead lock can sometimes occur: Files cannot be deleted even though they are no longer in use. Corresponding error messages in the web interface and the corresponding clients show that the file is locked.

The locks can also be verified via the occ CLI - for example for all users:

1$ ./occ files:scan --all
2Starting scan for user 1 out of 1337 (sgiertz)
3Starting scan for user 2 out of 1337 (ppinkepank)
4...
5Exception while scanning: "files/[...]" is locked
Note

For larger environments, it is recommended to run the scan only for affected users: ./occ files:scan [name]

One approach to solving the problem is to temporarily put Nextcloud into maintenance mode and remove the locks in the database:

1$ ./occ maintenance:mode --on
1$ mysql -u <nextcloud_user> -p <nextcloud_db>
2MariaDB [nextcloud_db]> DELETE FROM oc_file_locks;
1$ ./occ maintenance:mode --off

The scan should then show no more errors:

1$ ./occ files:scan --all
2Starting scan for user 1 out of 1337 (sgiertz)
3Starting scan for user 2 out of 1337 (ppinkepank)
4...
5+---------+-------+--------+--------------+
6| Folders | Files | Errors | Elapsed time |
7+---------+-------+--------+--------------+
8| 81      | 606   | 0      | 00:00:02     |
9+---------+-------+--------+--------------+

If these errors occur frequently, it is advisable to use a different locking mechanism - for example Redis.

In a docker-compose setup, it is sufficient to add another container for this purpose:

 1...
 2  nextcloud:
 3    image: nextcloud:28-apache
 4    container_name: nextcloud
 5    ports:
 6      - '8080:80'
 7    links:
 8      - nextcloud_db
 9      - nextcloud_redis
10    volumes:
11      - nextcloud:/var/www/html
12    env_file:
13      - env.nextcloud
14    networks:
15      - ext
16    depends_on:
17      - nextcloud_db
18      - nextcloud_redis
19    restart: always
20
21  nextcloud_redis:
22    image: redis:7.2
23    container_name: nextcloud_redis
24    command: "--appendonly yes --requirepass REDIS_PASSWORD"
25    restart: always
26
27  nextcloud_db:
28    image: mariadb:10.5
29    container_name: nextcloud_db
30    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
31    restart: always
32    volumes:
33      - nextcloud_db:/var/lib/mysql
34    env_file:
35      - env.nextcloud_db

REDIS_PASSWORD must be replaced by an appropriate, secure password. In this example, this must also be stored in the env.nextcloud file:

1REDIS_HOST=nextcloud_redis
2REDIS_HOST_PASSWORD=REDIS_PASSWORD

Translations: