Sorry for not following bug report model but the issue is also reported on nextcloud and fixed https://help.nextcloud.com/t/unable-to-delete-directories-text-file-busy/86468/3 Adding unset($it); as they say on forum post fixes it