Cannot delete directory error thrown by updater when upgrading 30.0.11 to 31.0.5

I tried to upgrade my nextcloud version 30.0.11 to 31.0.5 . I ran the upgrade via:

sudo -u www-data php /var/www/owncloud/updater/updater.phar --no-backup

Finally the upgrade fails with an error when trying to delete a directory:
/var/www/owncloud/data/updater-octpturgf05m/downloads/nextcloud/apps/settings

When I rerun the upgrade the story remains the same but wit a different directory that failed to delete. And when I try to delete the directory that the updater could not delete by using sudo -u www-data rmdir <directorypath> this works just fine . I also checked that all files in the upgrader directory belong to the apache user which is www-data on my ubuntu 24.04 system. For now I can return back to 30.0.11 because the cloud is running in a virtual machine that I snapshotted before upgrading, so I can start again from a running 30.0.11 system.

Rerunning updater.phar after it failed one time it fails again in the next run in the same step but for another directory. I tried 3 times and got each time an error for a different upgrader dir in each try:

 Could not rmdir /var/www/owncloud/data/updater-octpturgf05m/downloads/nextcloud/apps/settings
 Cloud not rmdir /var/www/owncloud/data/updater-octpturgf05m/downloads/nextcloud/apps/user_ldap/lib/Notification
 Could not rmdir /var/www/owncloud/data/updater-octpturgf05m/downloads/nextcloud/apps/files_versions/lib/Migration

The given directotry was always empty and could easily be remove by running

sudo -u www-data rmdir <path>

I also checked that there is enough free space on the filesystem.
Its really strange this error, no idea whats going on.

The following log is from /var/log/owncloud and shows the relevant part where the upgrader fails in one run:

2025-05-31T04:17:14+0000 6eSrdSe8D9 [info] storage location: /var/www/owncloud/data/updater-octpturgf05m/downloads/
2025-05-31T04:17:29+0000 6eSrdSe8D9 [info] end of verifyIntegrity()
2025-05-31T04:17:29+0000 6eSrdSe8D9 [info] endStep("5")
2025-05-31T04:17:29+0000 6eSrdSe8D9 [info] executeStep request for step "6"
2025-05-31T04:17:29+0000 6eSrdSe8D9 [info] startStep("6")
2025-05-31T04:17:29+0000 6eSrdSe8D9 [info] extractDownload()
2025-05-31T04:17:29+0000 6eSrdSe8D9 [info] storage location: /var/www/owncloud/data/updater-octpturgf05m/downloads/
2025-05-31T04:17:29+0000 6eSrdSe8D9 [info] Libzip version detected: 1.7.3
2025-05-31T04:24:46+0000 6eSrdSe8D9 [info] end of extractDownload()
2025-05-31T04:24:46+0000 6eSrdSe8D9 [info] endStep("6")
2025-05-31T04:24:46+0000 6eSrdSe8D9 [info] executeStep request for step "7"
2025-05-31T04:24:46+0000 6eSrdSe8D9 [info] startStep("7")
2025-05-31T04:24:46+0000 6eSrdSe8D9 [info] setMaintenanceMode("true")
2025-05-31T04:24:46+0000 6eSrdSe8D9 [info] configFileName /var/www/owncloud/updater/../config/config.php
2025-05-31T04:24:46+0000 6eSrdSe8D9 [info] end of setMaintenanceMode()
2025-05-31T04:24:46+0000 6eSrdSe8D9 [info] endStep("7")
2025-05-31T04:24:46+0000 6eSrdSe8D9 [info] executeStep request for step "8"
2025-05-31T04:24:46+0000 6eSrdSe8D9 [info] startStep("8")
2025-05-31T04:24:46+0000 6eSrdSe8D9 [info] replaceEntryPoints()
2025-05-31T04:24:46+0000 6eSrdSe8D9 [info] replace index.php
2025-05-31T04:24:46+0000 6eSrdSe8D9 [info] replace status.php
2025-05-31T04:24:46+0000 6eSrdSe8D9 [info] replace remote.php
2025-05-31T04:24:46+0000 6eSrdSe8D9 [info] replace public.php
2025-05-31T04:24:46+0000 6eSrdSe8D9 [info] replace ocs/v1.php
2025-05-31T04:24:46+0000 6eSrdSe8D9 [info] replace ocs/v2.php
2025-05-31T04:24:46+0000 6eSrdSe8D9 [info] end of replaceEntryPoints()
2025-05-31T04:24:46+0000 6eSrdSe8D9 [info] endStep("8")
2025-05-31T04:24:46+0000 6eSrdSe8D9 [info] executeStep request for step "9"
2025-05-31T04:24:46+0000 6eSrdSe8D9 [info] startStep("9")
2025-05-31T04:24:46+0000 6eSrdSe8D9 [info] deleteOldFiles()
2025-05-31T04:24:53+0000 6eSrdSe8D9 [info] config sample exists
2025-05-31T04:24:53+0000 6eSrdSe8D9 [info] themes README exists
2025-05-31T04:57:37+0000 6eSrdSe8D9 [info] end of deleteOldFiles()
2025-05-31T04:57:37+0000 6eSrdSe8D9 [info] endStep("9")
2025-05-31T04:57:37+0000 6eSrdSe8D9 [info] executeStep request for step "10"
2025-05-31T04:57:37+0000 6eSrdSe8D9 [info] startStep("10")
2025-05-31T04:57:38+0000 6eSrdSe8D9 [info] moveNewVersionInPlace()
2025-05-31T04:57:38+0000 6eSrdSe8D9 [info] storage location: /var/www/owncloud/data/updater-octpturgf05m/downloads/nextcloud/
2025-05-31T05:01:30+0000 6eSrdSe8D9 [error] executeStep request failed with other exception
2025-05-31T05:01:30+0000 6eSrdSe8D9 [error] Exception: Exception
Message: Could not rmdir /var/www/owncloud/data/updater-octpturgf05m/downloads/nextcloud/apps/settings
Code:0
Trace:
#0 phar:///var/www/owncloud/updater/updater.phar/lib/Updater.php(1012): NC\Updater\Updater->moveWithExclusions()
#1 phar:///var/www/owncloud/updater/updater.phar/lib/UpdateCommand.php(399): NC\Updater\Updater->moveNewVersionInPlace()
#2 phar:///var/www/owncloud/updater/updater.phar/lib/UpdateCommand.php(240): NC\Updater\UpdateCommand->executeStep()
#3 phar:///var/www/owncloud/updater/updater.phar/vendor/symfony/console/Command/Command.php(298): NC\Updater\UpdateCommand->execute()
#4 phar:///var/www/owncloud/updater/updater.phar/vendor/symfony/console/Application.php(1040): Symfony\Component\Console\Command\Command->run()
#5 phar:///var/www/owncloud/updater/updater.phar/vendor/symfony/console/Application.php(301): Symfony\Component\Console\Application->doRunCommand()
#6 phar:///var/www/owncloud/updater/updater.phar/vendor/symfony/console/Application.php(171): Symfony\Component\Console\Application->doRun()
#7 phar:///var/www/owncloud/updater/updater.phar/updater.php(12): Symfony\Component\Console\Application->run()
#8 /var/www/owncloud/updater/updater.phar(14): require('...')
#9 {main}
File:phar:///var/www/owncloud/updater/updater.phar/lib/Updater.php

Please note the base-directory is named owncloud, however the running software is really a nextcloud.

I had another idea but it didn’t proove true: on my Nextcloud-VM AppAmor was activated. Thought this could possibly prevent the system from removing directories. However after disabling AppAmor (Kernel-Module as well as the AppArmor service and a reboot), nothing changed when upgrading. Again I got the “cannot remove directory” error message during the “Move new files in place” step during the upgrade.