How to Empty Caches and Clean All Targets Xcode 4 and later
Asked Answered
S

16

266

Jonathan suggest here: Xcode Includes .xib files that have been deleted! that cleaning all targets and empty the caches will fix the problem with Xcode including deleted .xib files but I cannot find a way to empty the cache in Xcode 4.

How to do that in Xcode 4?

Striation answered 19/4, 2011 at 9:35 Comment(1)
Here's my [shell script solution][1], which deletes derived data, for both xCode 5 and 6. [1]: https://mcmap.net/q/16497/-how-to-delete-derived-data-and-clean-project-in-xcode-5-and-laterCreight
M
553

Command-Option-Shift-K to clean out the build folder. Even better, quit Xcode and clean out ~/Library/Developer/Xcode/DerivedData manually. Remove all its contents because there's a bug where Xcode will run an old version of your project that's in there somewhere. (Xcode 4.2 will show you the Derived Data folder: choose Window > Organizer and switch to the Projects tab. Click the right-arrow to the right of the Derived Data folder name.)

In the simulator, choose iOS Simulator > Reset Content and Settings.

Finally, for completeness, you can delete the contents of /var/folders; some caching happens there too.

WARNING: Deleting /var/folders can cause issues, and you may need to repair or reinstall your operating system after doing so.

EDIT: I have just learned that if you are afraid to grapple with /var/folders/ you can use the following command in the Terminal to delete in a more targeted way:

rm -rf "$(getconf DARWIN_USER_CACHE_DIR)/org.llvm.clang/ModuleCache"

EDIT: For certain Swift-related problems I have found it useful to delete ~/Library/Caches/com.apple.dt.Xcode. You lose a lot when you do this, like your spare copies of the downloaded documentation doc sets, but it can be worth it.

Mammalian answered 6/6, 2011 at 1:25 Comment(18)
Nice, cleaning the path: Library/Developer/Xcode/DerivedData + resetting the content and setting of the simulator works!Striation
Path appears to be in my home directory (note tilde): ~/Library/Developer/Xcode/DerivedData Maybe this was obvious to others, but in case someone else has the issue...Milurd
Hehe, being an übern00b I deleted /var/db where I found something that came from Xcode/my project. That cost me five hours of trashed journalling in the file system, repair and reinstall of Lion, etc. Be warned! :-)Louisiana
@Jonas haha... that's kinda funny.. unless you lost serious money from itAlitta
I have the same problem out of the blue. Have never had this issue before. I have done all of these suggestions to no avail. Delete app from device is the only solution for me. I do not want to delete the app every time I change a nib file. Has anybody found anything more conclusive for a fix? What is the /var/folders? I don't find it on my system.Musso
@matt: What type of data is stored in /var/folders. I would like to clean my stuff but deleting something in /var/ feels a bit risky to me.Limousine
@Limousine - Look and see. You'll find a lot of old .pch files cached there, for example. If you don't want to delete them, don't; I don't think it affects most people aside from taking up unnecessary disk space.Mammalian
I had to close my current project first before I delete the files in DerivedData and /var/folders before it worked! Also don't forget to reset the simulator before running the app again. :)Waistline
@Jonas I felt compelled to add a warning to the "delete /var/folders" advice. It's not for the uninitiated!Morphology
Agreed but then that would be a comment, surely - not a revision to the answer.Mammalian
I tried this in Xcode5 but it didn't work for me. I ended up reverting my whole changelist (using GitHUB) and that worked for me.Gand
For me, it was none of the other "XCode" caches, everything remained borken until I emptied /var/folders/ -- hooray for that one!Fop
Worth noting that 2014 in XCode 6 this is the(!) Solution if the debugger shows wrong lines or no data of members.Fahland
I ran into a situation where Xcode would not compile my project or backups of it because I had a Frameworks path in FRAMEWORK_SEARCH_PATHS. If you are hesitant to remove the contents of /var/folders, see my answer at https://mcmap.net/q/16794/-39-__strong-39-only-applies-to-objective-c-object-or-block-pointer-types-type-here-is-xxx-quot-warning for a full explanation of how working project settings can suddenly fail if they affect shared PCH. My best guess is that sometimes Xcode's caches don't rebuild the same way they (luckily) did before, so longstanding misconfigurations can be masked but eventually reveal themselves.Vibrate
Do not run rm -rf /var/folders with sudo or you will end up repairing as myself :)Deboer
Works with Xcode 7.0! I lost almost 2 days trying to delete and all version of a module. I executed all the commands in the answer and it is gone at last! Thank you!Hildegaard
I had accidentally removed /var/folders before reading all the warnings. my macbook hangs forever after the reboot. I have to boot up in single user mode, remount the whole directory before remove and recreate the directory folders/zz in /var. osxbytes.wordpress.com/2015/03/19/…Balanced
@matt, I think you need to add a warning about /var/folders here. Great answer, but mandating that the warning 'would be a comment, not a revision to the answer' is causing harm.Ramunni
S
77

Command-Option-Shift-K should do it. Alternatively, go to product menu, press the option key, now the option "Clean" will change to "Clean Build Folder ..." select that option.

Smokeproof answered 25/4, 2011 at 4:21 Comment(3)
Nope, too bad.. i just changed and renamed an xib file and cleaned the app as you said but it still loads fine without errors.Striation
Wow, never new about that alt key trick for Mac OS X menus!!! That really defines the meaning of "Alt"ernate.Kv
Hello @sram, the Command-Option-Shift-K not working on the new xCode (12.5) You have something else ?Systematic
T
26

In addition to doing the following, you may experience this issue if your app's Storyboard's files are localized. First, do each of these:

  • Clean Build
  • Reset Simulator
  • Restart Xcode
  • Delete your DerivedData folder in ~/Library/Developer/Xcode/DerivedData
  • Restart Computer

What finally worked for me was re-generating the storyboard localization files and deleting the English localization (which was unneeded because the Base Locale is English). To reload your Storyboard strings, see this answer on StackOverflow.

I had noticed that the non-text parts of my Storyboard were being updated, but not the localized text. If you have a localization in your app, I would recommend checking to make sure your localizations are updated (even if they're .strings files).

Tragicomedy answered 27/10, 2013 at 3:13 Comment(4)
Deleting the contents (not the whole folder) of /var/folders did it for me! If you're looking for the /var/folders...Choose Go to Folder from the Finder's Go menu, and enter /var/ as the folder's location.Lice
+1 on the /var/folders instruction... cleaning and deleting DerivedData worked partially but that one did the trickCurb
1. Clean Build 2. Reset Simulator 3. Restart Xcode That solved my issue.Horseshoes
WARNING! Beware that deleting contents from /var/folders/ can cause serious issues and might result in having to completely reinstall OSx from the recovery console!Engen
P
18

I found another way in addition to command+option+shift+K. In XCode 4.2 there is an organizer that can be opened from top-right icon. You can clean all archives and saved project options from there. This helped my situation (I was seeing old removed files in the mainBundle).

Poco answered 23/4, 2012 at 13:45 Comment(2)
Yeah that's indead an easier way than cleaning ~/Library/Developer/Xcode/DerivedData manuallyStriation
It's cmd+opt+shift+O nowEveretteverette
A
10

I have been pulling out hair from my head because I thought that I had the same problem. When building the app I didn't get the same result on my iPhone as on the simulator.

The problem was that I had somehow made a localized version of the MainStoryboard.storyboard file. So when I ran the app on my phone it showed the danish version... and the simulator showed the english version.

Yeah I'm new! :)

Amative answered 19/12, 2011 at 2:7 Comment(2)
I had similar problems. Did command-option-shift-K to no avail. Removed ...DerivedData, no solution. Turned out changing the top level XIB only changes the English version, and I was testing with Dutch version of the XIB. Sigh...Uzziel
Also, be aware that there are ways to localize your app where separate text files are created for each language. In such cases, changing the .xib doesn't do a thing.Infiltrate
L
7

To delete all derived data and the module cache in /var/folders use this little ruby script.

derivedDataFolder = Dir.glob(Dir.home + "/Library/Developer/Xcode/DerivedData/*")
moduleCache = Dir.glob("/var/folders/**/com.apple.DeveloperTools*")
FileUtils.rm_rf derivedDataFolder + moduleCache

This just solved a fatal error: malformed or corrupted AST file: 'Unable to load module "/var/folders/ error for me.

Luminosity answered 16/10, 2013 at 16:14 Comment(1)
The apparent bug causing "fatal error: malformed or corrupted AST file: 'Unable to load module", and the failure of the gentle solution suggested by Xcode, is still occurring in Xcode 6.1. And the solution to quit Xcode and manually clean out ~/Library/Developer/Xcode/DerivedData manually still works. I didn't need to clean everything in there; just two subfolders: ModuleCache, and the subfolder whose name is relevant to the problem project.Cardiograph
K
6

I had some problems with Xcode 5.1 crashing on me, when I opened the doc window.

I am not sure of the cause of it, because I was also updating docsets, while I opened the window.

Well, in Xcode 5 the modules directory now resides within the derived data folder, which I for obvious reasons didn't delete. I deleted the contents of ~/Library/Developer/Xcode/DerivedData/ModuleCache and the ~/Library/Preferences/com.apple.Xcode.plist and everything then seems to work, after I restarted Xcode.

Kheda answered 13/3, 2014 at 1:0 Comment(0)
C
4

My "DerivedData" with Xcode 10.2 and Mojave was here:

MacHD/Users/[MyUser]/Library/Developer/Xcode

Clarinda answered 1/7, 2019 at 9:17 Comment(0)
B
3

Simply delete Derived Data from Terminal,

   rm -rf ~/Library/Developer/Xcode/DerivedData/*
Boogie answered 21/10, 2022 at 15:32 Comment(0)
Y
1

When using a "Data Model" , there are options in the inspector to generare classes, for me this was the case as there was already a class with the existing name.

Codegen: solved it for me.

enter image description here

Yellowish answered 12/7, 2017 at 19:55 Comment(0)
S
0

You have to be careful about the xib file. I tried all the above and nothing worked for me. I was using custom UIButtons defined in the xib, and realized it might be related to the fact that I had assigned attributes there which were not changing programmatically. If you've defined images or text there, remove them. When I did, my programmatic changes began to take effect.

Sediment answered 18/2, 2014 at 15:22 Comment(0)
C
0

Here's my shell script solution, which deletes derived data and cleans a project's cached assets, for Xcode 4, 5 and 6.

Sometimes, simply calling rm -rf on the Derived Data directory leaves a lingering file or two, but my script loops until all files are deleted.

Creight answered 16/1, 2015 at 6:55 Comment(0)
T
0

I tried almost everything but could not help,what helped me was disabling SIP(System Integration Protection) n it works,I do not know what happens when and how but system file get confused somewhere and causes this...though there might be risks to disabling this obviously

here is how

1.Power on your Mac and hold down the [command]+[R] keys to access the Recovery Partition.

2.From the Recovery Partition, click Utilities from the menu bar, and then select Terminal.

3.Enter the following command into Terminal and press Enter to execute it: csrutil disable

4.Once the command has executed, exit the Terminal and reboot the Mac. When you log back into OS X, SIP will be disabled.

Thunderclap answered 19/10, 2016 at 11:34 Comment(2)
Sounds risky indeed. What does SIP do?Striation
System Integrity Protection protects system files and directories that are flagged for protection.basically adds more security to your system,limiting even the power of the sudo commandThunderclap
L
0

For me with cached images helped: Xcode -> Product -> Clean Build Folder

Leonialeonid answered 19/2, 2022 at 18:6 Comment(0)
R
0

For convenience reasons, I created an apple script for this routine

tell application "Xcode"
    activate
    
    set targetProject to active workspace document
    repeat 60 times
        if loaded of targetProject is true then
            exit repeat
        end if
        delay 0.5
    end repeat
    
    stop targetProject
    
    set action to clean targetProject
    repeat 60 times
        if completed of action is true then
            exit repeat
        end if
        delay 0.5
    end repeat
    
    quit
end tell

set cacheDir to do shell script "getconf DARWIN_USER_CACHE_DIR"
set whoAmI to do shell script "whoami"

tell application "Finder"
    try
        delete folder (cacheDir & "clang/ModuleCache")
    end try
    try
        delete folder (cacheDir & "clang." & whoAmI & "/ModuleCache")
    end try
    try
        delete every item of folder (cacheDir & "org.llvm.clang/ModuleCache")
    end try
    try
        delete every item of folder (cacheDir & "org.llvm.clang." & whoAmI & "/ModuleCache")
    end try
    try
        delete every item of folder ((path to home folder as text) & "Library:Developer:Xcode:DerivedData")
    end try
    try
        delete every item of folder ((path to home folder as text) & "Library:Caches:com.apple.dt.Xcode")
    end try
end tell

tell application "Xcode" to activate

Then I exported the script as an application

And now I can just press ⌘+Space and type "Xcode reset" in any case of unusual Xcode behaviour.

I also created a gist here

Repudiation answered 6/3 at 10:51 Comment(0)
V
-3

You can use PrettyClean to clean the all of dev tools caches including Xcode.

Steps:

Veinule answered 8/3, 2022 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.