Defrag to improve Synology BTRFS performance

My Synology NAS gets a lot of use at home. Not only is the Synology NAS a central point for my VMware home lab, but all my PCs are connected to it for document, photo, and ISO storage.

In order to keep things as optimized as possible I try to defrag the drives every once in a while. Depending on how fragmented the drives are, this could help improve performance in sequential read and random IOPS.

In this post I’ll show how it easy it is to kick off the task. Just keep in mind you will want to do this during low usage as the defrag task will certainly impact performance while it’s running.

How to defrag Synology NAS using BTRFS

  1. Log into your Synology DSM.
  2. Open Storage Manager (found under the “Start Menu”) then click on Volume > Manage.
    synology defrag storage manager
  3. Next select Start file system defragmentation and click on Next.
    defrag synology volume wizard
  4. Now click on Apply.
    synology defrag confirm wizard

How to schedule the Synology defrag

If you would rather this be an automated process you can schedule the defrag instead.

  1. Log into your Synology DSM and go to Control Panel.
  2. Go to Task Scheduler and click on Create > Scheduled Task > User-defined script.
    synology defrag schedule task create
  3. Under the General tab give the task a name and keep the user set to root. Then under the Schedule tab configure when you want the defrag to run. On the Task Settings tab enter the following command:
    /sbin/btrfs filesystem defragment -r /volume1

    synology defrag schedule task command


Finally you’ll be taken back to the Storage Manager and will see a message stating that defrag has started. Depending on how fragmented your drives are this could take several minutes to a couple hours.

synology defrag

Post in the comments below if you’ve noticed any speed improvements as well after a defrag.

This Post Has 7 Comments

  1. Thanks for this 🙂

  2. Thanks for posting this. It is exactly what I’ve been looking for. However, the script fails with the following errors. Any idea why?

    Standard output/error:
    ERROR: defrag failed on /volume1/@appstore/Node.js_v4/usr/local/bin/node: Success
    ERROR: defrag failed on /volume1/@appstore/SynologyApplicationService/daemon/pgbouncer: Success
    ERROR: defrag failed on /volume1/@appstore/SynologyApplicationService/tools/notification_send: Success
    ERROR: defrag failed on /volume1/@appstore/ChannelsDVR/channels-dvr/2018.11.20.2224/ffmpeg: Success
    ERROR: defrag failed on /volume1/@appstore/ChannelsDVR/channels-dvr/2018.11.20.2224/channels-dvr: Success
    ERROR: defrag failed on /volume1/@appstore/CloudSync/sbin/syno-cloud-syncd: Success
    ERROR: defrag failed on /volume1/@appstore/Plex Media Server/Plex Media Server: Success
    ERROR: defrag failed on /volume1/@appstore/Plex Media Server/Plex Script Host: Success
    ERROR: defrag failed on /volume1/@appstore/Plex Media Server/Plex Tuner Service: Success
    total 9 failures

  3. Yeah. I’m getting those errors too. Any suggestions?

  4. me too

  5. To get rid of those errors, specify the directories to defrag like the webapp does. Watching ps aux it cycles individually through all shared folders excluding the @ActiveBackup. Throw a -v in there to get verbose and email output from the task. The -b is an exclude. This example would have run in 3 commands but can be combined:

    /sbin/btrfs filesystem defragment -v -r -b /volume1/nacho/@ActiveBackup /volume1/nacho -b /volume1/burrito/volume1/burrito/@ActiveBackup /volume1/burrito -b /volume1/taco/@ActiveBackup /volume1/taco

    1. Only you know how YOUR NAS is configured and should research how things may impact YOUR configuration. I don’t use snapshots or deduplication on my NAS so neither of these are an issue for me.

      I would suggest you read the Synology Knowledgebase, https://www.synology.com/en-us/knowledgebase/DSM/help/DSM/StorageManager/volume_filesystem_defragmentation – at no point does it mention you should NOT run this command. In fact they state “Volume usage might increase if the volume contains snapshots of shared folders.”.

      -Michael

Leave a Reply