Marco Eidinger
Swifty Tech by Marco Eidinger

Swifty Tech by Marco Eidinger

swift package purge-cache

Photo by Brett Jordan on Unsplash

swift package purge-cache

Marco Eidinger's photo
Marco Eidinger
·Mar 24, 2022·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

I explained in a previous article when to use swift package reset

This will reset the complete cache/build directory. For SPM (command-line) this will affect the .build folder.

The equivalent Xcode option is named "Reset Package Caches". This will affect the SourcePackages of the related folder in DerivedData.

If you run swift package help then you will see another subcommand called purge-cache. The description explains that the subcommand will Purge the global repository cache.

What is this global repository cache and why does it exist?

Apple Swift Package Manager introduced this cache, to improve the handling package dependencies used in multiple packages, in Swift 5.4.

Swift Package Manager now caches package dependency repositories on a per-user basis, which reduces the amount of network traffic and increases performance of dependency resolution for subsequent uses of the same package.

Default location on macOS is /Users/<YourUserName>/Library/Caches/org.swift.swiftpm/repositories

A quick summary: a package dependency, even when used in various packages, gets cloned only once into the cache and then source code can be copied to the .build cache when swift package resolve runs on a package using this package dependency. Making resolving faster.

You can learn more by reading the source code or the related pull request.

When would you need to purge this global repository cache?

Only in extreme cases, for example when the repository structure gets irreversible re-structured.

In most cases swift package reset + swift package resolve or swift package update are sufficient.

It is NOT necessary to purge the global repository cache even in situations in which your package dependency is pinned to a specific version and this tag gets deleted in the upstream repo and then re-created on a new commit. You can run swift package reset and delete Package.resolved file to pull in the expected changes with the next swift package resolve.

Did you find this article valuable?

Support Marco Eidinger by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this