How to delete .DS_STORE files in current folder and all subfolders

macOS automatically creates .DS_Store files in every folder you open in Finder. They're harmless on your own machine but are notorious for cluttering Git repositories. This guide covers how to delete them, keep them out of Git, and prevent future pollution.


What Is a .DS_Store File?

DS_Store stands for Desktop Services Store. macOS creates this hidden file in each directory to store metadata about the folder's Finder view: icon positions, window size, background image, sort order, and other display preferences.

The file is invisible in Finder by default (it starts with a dot), but it shows up in Git diffs and git status output, where it creates noise and accidental commits.


Delete All .DS_Store Files in the Current Folder

Run this command from your project's root directory to recursively find and delete every .DS_Store file:

find . -name ".DS_Store" -delete

The find command searches from the current directory (.) through all subdirectories. -name ".DS_Store" matches the filename exactly, and -delete removes each match.

To see what would be deleted before actually deleting (a dry run):

find . -name ".DS_Store"

This lists all matching files without removing them. Once you're confident, add -delete to do the cleanup.


Delete .DS_Store Files from a Specific Directory

# Delete from a specific path
find /Users/yourname/projects/myapp -name ".DS_Store" -delete

# Delete from your entire home directory
find ~ -name ".DS_Store" -delete

Remove .DS_Store Files Already Committed to Git

If .DS_Store files were committed before you added them to .gitignore, deleting them from disk isn't enough — they remain in Git's history and will keep reappearing. You need to untrack them:

# Remove from Git's index (stops tracking), but keep the file on disk
git rm --cached .DS_Store

# Remove all .DS_Store files from tracking recursively
git rm --cached -r --ignore-unmatch "**/.DS_Store"

# Then commit the removal
git commit -m "Remove .DS_Store files from tracking"
Note: --cached removes the file from Git's index without deleting it from your filesystem. Without --cached, Git would delete the file from disk too.

Prevent .DS_Store from Being Committed — .gitignore

Add .DS_Store to your project's .gitignore to prevent it from ever being committed:

# .gitignore
.DS_Store
**/.DS_Store

The **/.DS_Store pattern matches .DS_Store in any subdirectory, not just the root. Commit the .gitignore change so your whole team benefits.


Global .gitignore — Apply to All Your Repositories

If you work on many projects, set a global .gitignore so you never have to think about this again:

# Create (or edit) the global gitignore file
echo ".DS_Store" >> ~/.gitignore_global

# Tell Git to use it for all repositories
git config --global core.excludesfile ~/.gitignore_global

This applies to every repository on your machine, without touching each project's own .gitignore.


Other macOS Clutter Files Worth Ignoring

While you're at it, these other macOS artifacts are also worth adding to your .gitignore:

# macOS metadata and system files
.DS_Store
**/.DS_Store
.AppleDouble
.LSOverride
._*

# macOS Spotlight index files
.Spotlight-V100
.Trashes

# macOS icon files
Icon?

Why .DS_Store Files Matter in Git

Leaving .DS_Store files unignored causes several practical problems:

  • Polluted git status — they appear as untracked files constantly
  • Accidental commits — developers commit them without realizing
  • Merge conflicts — two developers editing Finder view preferences in the same folder creates a merge conflict on a file with no meaningful content
  • Repository bloat — they accumulate over time if not cleaned up
  • Security consideration.DS_Store files can leak folder structure information about your machine

Summary

Run find . -name ".DS_Store" -delete to clean up existing files. Add .DS_Store to .gitignore (or a global ~/.gitignore_global) to prevent future commits. If the files were already committed, use git rm --cached to untrack them without deleting them from disk.

No comments :

Post a Comment

Please leave your message queries or suggetions.

Note: Only a member of this blog may post a comment.