<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Rsync on KnightLi Blog</title>
        <link>https://knightli.com/en/tags/rsync/</link>
        <description>Recent content in Rsync on KnightLi Blog</description>
        <generator>Hugo -- gohugo.io</generator>
        <language>en</language>
        <lastBuildDate>Sun, 29 Mar 2026 11:00:00 +0800</lastBuildDate><atom:link href="https://knightli.com/en/tags/rsync/index.xml" rel="self" type="application/rss+xml" /><item>
        <title>rsync --delete Explained and Practical Directory Cleanup</title>
        <link>https://knightli.com/en/2026/03/29/rsync-delete-explained/</link>
        <pubDate>Sun, 29 Mar 2026 11:00:00 +0800</pubDate>
        
        <guid>https://knightli.com/en/2026/03/29/rsync-delete-explained/</guid>
        <description>&lt;p&gt;The core purpose of &lt;code&gt;rsync --delete&lt;/code&gt; is to remove files in the target directory that do not exist in the source directory, so both sides stay consistent.&lt;/p&gt;
&lt;p&gt;Typical use cases include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Cleaning stale files on the target side during sync&lt;/li&gt;
&lt;li&gt;Quickly emptying a target directory by syncing from an empty source directory&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;basic-syntax&#34;&gt;Basic Syntax
&lt;/h2&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;rsync -a --delete source_dir/ target_dir/
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;&lt;code&gt;-a&lt;/code&gt;: archive mode, preserves permissions, timestamps, and other attributes&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--delete&lt;/code&gt;: removes extra files on the target side&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Important note: whether &lt;code&gt;source_dir&lt;/code&gt; ends with &lt;code&gt;/&lt;/code&gt; changes behavior. With &lt;code&gt;/&lt;/code&gt;, rsync syncs directory contents; without &lt;code&gt;/&lt;/code&gt;, it syncs the directory itself.&lt;/p&gt;
&lt;h2 id=&#34;quickly-empty-a-target-directory-with-an-empty-source&#34;&gt;Quickly Empty a Target Directory with an Empty Source
&lt;/h2&gt;&lt;p&gt;If your goal is to keep the directory path but clear all contents, use an empty directory as the source:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;# 1) Create an empty directory&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;mkdir -p /tmp/empty_dir
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;# 2) Sync and delete target-side content&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;rsync -a --delete /tmp/empty_dir/ /path/to/target_dir/
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;In large-directory scenarios, this is often more efficient than deleting files one by one, and it is easier to automate in scripts.&lt;/p&gt;
&lt;h2 id=&#34;common-extended-options&#34;&gt;Common Extended Options
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;code&gt;--delete-before&lt;/code&gt;: delete before transfer, which can be faster in some cases&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--progress&lt;/code&gt;: show transfer and processing progress&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Example (cleaning an Nginx log directory):&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;rsync -a --delete --progress /tmp/empty_dir/ /var/log/nginx/
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h2 id=&#34;recommendations&#34;&gt;Recommendations
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;Run with &lt;code&gt;--dry-run&lt;/code&gt; first to verify the deletion scope&lt;/li&gt;
&lt;li&gt;Back up the target directory before running in production&lt;/li&gt;
&lt;li&gt;For critical paths, schedule execution during off-peak hours&lt;/li&gt;
&lt;/ul&gt;
</description>
        </item>
        
    </channel>
</rss>
