How to delete ignored files in syncthing (unofficial, with caveats)

So, this is a technique I’ve been using for some years now. I’m posting this here because it appears that this is asked quite frequently, so I wanted to at least document it.

This shows how I automatically delete ignored files. A typical example are cache/temp files: Originally I forgot to ignore them, so they synced. Then I ignored them, but I still have old cache files laying around I want gone.

Disclaimer: This technique is not officially supported and not without caveats. There is a risk of loosing data if you don’t know what you’re doing. You have been warned.

To better explain the situation, lets make some assumptions about the setup. This technique also works in various different setups, but I believe that showcasing an example is much easier to understand.

Setup: Three devices (“PC”, “Laptop”, “Server”) that all share a single folder.

Situation: Some files have been ignored on “PC”, but they have already synced to “Laptop” and “Server”.

Goal: Automatically delete the files on devices “Server” and “Laptop” that have been ignored on “PC”.

initial

Imagine the above folder layout. Three normal files are there, and two files that should be ignored. Those have already been ignored on “PC”. However, they are already synced to the other devices and are now lingering around. The Global state on all devices hence looks like this:

global-state-before

Prerequisite: All devices are online and up-to-date. I highly recommend to not change any files during this process - if you’re not careful, those changes can get lost.

Step 1: For safety we’re going to flip “PC” to send-only. This allows us to undo accidental deletions if something goes wrong. However, changes from “Server” and “Laptop” won’t be received during this period.

Step 2: Ignore the to-be-deleted files on both “Server” and “Laptop”. If done correctly, the global state will drop down by the number of ignored files (and folders) to indicate that the ignored files are not valid anywhere.

global-state-reduced

In the above screenshot, we see that the global state has dropped from 5 to 3, because the two ignored files are now ignored everywhere.

Step 3: Flip the folder type on “Server” and “Laptop” to Receive-only, if they aren’t already. This is important, because we’re going to rely on the revert local changes feature for deletion.

Step 4: With the folders still in Receive-only, remove the ignore patterns from “Server” and “Laptop”. If you only want to delete a portion of the files, only remove the ignore patterns for the files you want to delete.

local-additions-are-previously-ignored-files

Note how the local state is now larger than the global state, because this node has un-ignored files that are ignored everywhere else. Because it’s in receive-only, it won’t send out the “changes” we made to the ignored files.

Step 5: The previously-ignored files will now show up as “Local Additions”. You can click on “locally changed items” to view them. In our setup, this will show the two ignored_file_{1,2}.mkv files. Ensure that everything is okay and you really want those files gone.

Step 6: Press the big red “Revert Local Changes” button shown above. This will delete the ignored files.

If the ignored files are deleted (local state drops down to the global state), all is good! You have succesfully deleted the ignored files/folders.

Troubleshooting note: I have encountered cases where pressing “Revert Local Changes” did not delete any files. Instead, the local state jumped back up the global state as if nothing happened. I believe that this is a bug in syncthing. In such cases, I had to perform an index exchange first: This can be done by removing the folder on any device and then readding it (with the same path, ID & settings). Alternatively, you can try starting syncthing with --reset-deltas command line option on any device.

Afterwards, once the folder has scanned and synced everywhere. Try “Revert Local Changes” again. Now the files should be deleted.

Step 7: Once everything has been deleted, revert all folders back to their original settings (send-receive for example) and restore the proper ignore patterns. Sync now continues as usual.

4 Likes