Skip to content

Empty directories accumulate in local preview storage after preview files are deleted #46425

Open

Description

@petruchito

class BackgroundCleanupJob extends TimedJob {

This should delete empty preview directories as well.
After running a few weeks with automatic ZFS snapshots folder attached as a history (It was a bad idea),
I've got a monstrous directory structure and the cron scan constantly utilized my disk for several days(SELECT-UPDATE on the oc_filecache table).

files:cleanup didn't do anything, 0 orphaned files.

files:scan-app-data took almost 6 hours to complete:

+---------+--------+--------------+
| Folders | Files  | Elapsed time |
+---------+--------+--------------+
| 5868385 | 151476 | 05:45:49     |
+---------+--------+--------------+

So I'm now deleting empty folders manually with find . -type d -empty -delete in the preview directory.

Activity

  1. petruchito commented on Jul 11, 2024

    @petruchito
    Author

    occ files:scan-app-data after deleting empty directories:

    +---------+--------+--------------+
    | Folders | Files  | Elapsed time |
    +---------+--------+--------------+
    | 368149  | 151478 | 02:13:46     |
    +---------+--------+--------------+
    
  2. szaimen commented on Jul 11, 2024

    @szaimen
    Contributor

    Hi, which nc version?

  3. petruchito commented on Jul 11, 2024

    @petruchito
    Author

    Hi, which nc version?

    Nextcloud AIO v9.1.0
    (NEXTCLOUD_VERSION | 29.0.3)

  4. petruchito commented on Jul 11, 2024

    @petruchito
    Author

    Snapshots were ~400Gb each, 24 hourly, 7 daily, 4 weekly and 12 monthly, so every hour the server was introduced with 400Gb of data and 400Gb disappeared. IO utilization issues persisted after I unmounted the history.

    Folders structure under the `history/` was like this:
    initial
    zfs-auto-snap_daily-2024-06-07-00h07
    zfs-auto-snap_daily-2024-06-08-00h07
    zfs-auto-snap_daily-2024-07-04-00h07
    zfs-auto-snap_daily-2024-07-05-00h07
    zfs-auto-snap_daily-2024-07-08-00h07
    zfs-auto-snap_daily-2024-07-11-00h07
    zfs-auto-snap_daily-2024-07-12-00h07
    zfs-auto-snap_hourly-2024-05-18-16h00
    zfs-auto-snap_hourly-2024-05-18-17h00
    zfs-auto-snap_hourly-2024-05-18-18h00
    zfs-auto-snap_hourly-2024-05-18-19h00
    zfs-auto-snap_hourly-2024-05-18-20h00
    zfs-auto-snap_hourly-2024-05-18-21h00
    zfs-auto-snap_hourly-2024-05-18-22h00
    zfs-auto-snap_hourly-2024-05-18-23h00
    zfs-auto-snap_hourly-2024-06-04-01h00
    zfs-auto-snap_hourly-2024-06-04-04h00
    zfs-auto-snap_hourly-2024-06-04-10h00
    zfs-auto-snap_hourly-2024-06-04-12h00
    zfs-auto-snap_hourly-2024-06-04-14h00
    zfs-auto-snap_hourly-2024-06-05-14h00
    zfs-auto-snap_hourly-2024-06-05-21h00
    zfs-auto-snap_hourly-2024-06-05-23h00
    zfs-auto-snap_hourly-2024-06-06-04h00
    zfs-auto-snap_hourly-2024-06-07-04h00
    zfs-auto-snap_hourly-2024-06-07-11h00
    zfs-auto-snap_hourly-2024-07-04-19h00
    zfs-auto-snap_hourly-2024-07-07-14h00
    zfs-auto-snap_hourly-2024-07-07-19h00
    zfs-auto-snap_hourly-2024-07-07-21h00
    zfs-auto-snap_hourly-2024-07-12-00h00
    zfs-auto-snap_monthly-2024-04-01-00h28
    zfs-auto-snap_monthly-2024-05-01-00h28
    zfs-auto-snap_monthly-2024-06-01-00h28
    zfs-auto-snap_monthly-2024-07-01-00h28
    zfs-auto-snap_weekly-2024-05-12-00h14
    zfs-auto-snap_weekly-2024-05-19-00h14
    zfs-auto-snap_weekly-2024-06-09-00h14
    zfs-auto-snap_weekly-2024-07-07-00h14
    # and 
    /Documents/...
    /Pictures/...
    # under each directory of above
    

    And this might also relate to the problem - I have 'filesystem_check_changes' => 1 in the config.php.

  5. joshtrichards commented on Jul 19, 2024

    @joshtrichards
    Member

    I don't quite understand your bug report and you seem to have entirely bypassed our reporting template.

    I understand your suggestion about the background job, but what was the originally problem that arose?

    There are (or were) some bugs like #45654. Is this merely that?

  6. petruchito commented on Jul 20, 2024

    @petruchito
    Author

    what was the originally problem that arose?

    Constant disk utilization by nextcloud's postgres led me to finding 5M folders in the preview folder.
    The cleanup job seems not to remove all empty folders and they were building up there.

    (#45654 does not seem to relate.)

  7. petruchito commented on Jul 20, 2024

    @petruchito
    Author

    I suppose this deletes preview/b/5/7/7/2/e/7/39548/ but leaves preview/b/5/7/7/2/e/7/ orphaned even if it's an empty directory.

    Thus you theoretically can end up with 7^16=3,32×10^13 empty directories.

  8. oTTo-oTTo commented on Oct 23, 2024

    @oTTo-oTTo

    I plead for 16⁷ directories 2.7E08
    and yes, the empty ones should be deleted.

    NC29.0.4: if have 1300 empty preview dirs in 3 months.

  9. marcan commented on May 20, 2025

    @marcan

    Same problem here, and this is particularly painful on CephFS where folders aren't cheap (they each create a metadata object and scanning them is slow if too many exist to fit in MDS cache).

    The cleanup should be walking up the folder tree and trying to delete any empty folders. I'm doing the find thing in a cron job to clean up empty folders now, but I don't know if this will confuse nextcloud since the empty folders are still cached in oc_filecache. Presumably they should be deleted from there too.

    Edit: Trying this now, hopefully it's a bit safer:

            find appdata_ocfkiy9cio27/preview -type d -mtime +7 -empty -print -delete | \
            sed --unbuffered "s/.*/delete from oc_filecache WHERE path = '\0';/" | \
            su -c "psql -U postgres nextcloud" postgres
    
  10. changed the title [-]Empty directories building up in the `preview` folder.[/-] [+]Empty directories accumulate in local preview storage after preview files are deleted[/+] on Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions