Okay, so there is at least 2 solution to this problem
The first one, given by @Ole will first find and delete files. Then it will look for files needed to be copied, and as soon as that happens, the sync will stop. It is easy and reliable, we just add these flags: --delete-before
and --max-transfer=1B
to our sync operation like this:
rclone sync source: dest: --delete-before --max-transfer=1B
--delete-before
: Will delete files before copying any files
--max-transfer=1B
: Will stop the operation after 1 byte has been transferred
This will also mean, that if you got many files, it will keep looking for a file that needs to be transferred, and only when that happens it will stop the operation. So to actually ONLY remove files, and not spend the time looking for a file that can be transferred, we can instead use the solution given by @ncw which is a bit more complicated, and only works in Linux (might be able to be ported for Windows also)
We will first need to save a sorted list of all files in source and destination like this:
rclone lsf --files-only -R source: | sort > src.txt
rclone lsf --files-only -R destination: | sort > dst.txt
rclone lsf
: List objects and directories in easy to parse format
--files-only
: Only list files
-R
: Recursive
| sort > src.txt
: Pipe to sort, and save as txt file (Only on Linux)
Next we will compare the 2 text files. We want to find lines in 'dst.txt' that is not in 'src.txt', and for that we can use comm
(Linux) like this:
comm -13 src.txt dst.txt > to-delete.txt
-13
: Return the files that are in the 'dst.txt' but not in the 'src.txt'
> to-delete.txt
: Save the result in a txt file
Now we will only need to delete the files in 'to-delete.txt' and to do that we use rclone delete
with --files-from
and --rmdirs
flags like this:
rclone delete --dry-run --files-from to-delete.txt --rmdirs destination:
--dry-run
: Test run. When satisfied, remove this flag to actually delete the files
--files-from to-delete.txt
: Only delete files within 'to-delete.txt'
--rmdirs
: Remove empty directories
These are not my own findings, and I would never have found this out without @Ole and @ncw - A big tank you to you
I have written the complete bash code based on the answer from @ncw
#!/bin/bash
# Where to save temp files?
temp_files_path="~/temp_files"
mkdir -p "$temp_files_path"
# Source drive + path
source_path="source:my/path"
# Destination drive + path
destination_path="destination:my/path"
echo "Listing files on source (${source_path})"
rclone lsf --files-only -R ${source_path} | sort > "$temp_files_path/src.txt"
echo "Listing files on destination (${destination_path})"
rclone lsf --files-only -R ${destination_path} | sort > "$temp_files_path/dst.txt"
echo "Comparing source files with destination files"
comm -13 "$temp_files_path/src.txt" "$temp_files_path/dst.txt" > "$temp_files_path/to-delete.txt"
echo "Files to delete from ${destination_path}"
echo "-----------------------------"
cat "$temp_files_path/to-delete.txt"
echo "-----------------------------"
read -p "Click on 'Enter' to delete the above files on ${destination_path}"
echo "Deleting the files on ${destination_path}"
rclone delete --files-from "$temp_files_path/to-delete.txt" --rmdirs ${destination_path}
# Clean up
echo "Removing temp dir"
rm -r "$temp_files_path"
echo "All done"
exit 0
You can just copy & paste the code, edit the 3 variables (temp_files_path, source_path, destination_path) to your needs and save as delete.sh. Then you need to run chmod +x delete.sh
before finally running the file with this command: ./delete.sh
. The program will list the files to be deleted, and waiting for your confirmation before starting to delete anything