176

How can I delete an old Data Model in Xcode? The option is disabled on the menu. (The models I want to delete have not been released to the public - they are interim development models.)

David Avendasora
  • 4,538
  • 1
  • 16
  • 15
SAHM
  • 4,078
  • 7
  • 41
  • 77
  • Do you mean the "Delete" choice under the "Edit" menu is disabled (while your old Data Model file is selected in the File inspector)? Or do you mean something else altogether? – Michael Dautermann Oct 10 '11 at 21:21
  • Yes, the delete choice under the Edit menu, and also under the control-right-click menu (and any menu I can find) is disabled. I know some people are going to a previous xcode to delete old/unused versions, but I don't have one installed. – SAHM Oct 10 '11 at 21:23
  • Michael, are you able to do this in xCode 4? – SAHM Oct 11 '11 at 00:45
  • yes... I've never not seen that "delete" menu option disabled for any of my XCode 4 projects. – Michael Dautermann Oct 11 '11 at 00:59
  • Sepcifically for the xcdatamodel? Others have had the same problem with deleting xcdatamodels XCode 4.. I just haven't seen a solution posted. – SAHM Oct 11 '11 at 01:43
  • Clarification - I can delete an entire data model, but I cannot delete individual versions of the model in Xcode 4. I tried making the whole project a workspace, as I read somewhere that that would enable the delete, but it did not work. – SAHM Oct 12 '11 at 12:56
  • Looks like **4.4** also does not offer a clean way to delete 'em... – Carelinkz Jul 28 '12 at 18:14
  • This answer is much simpler (just a couple of file drags) http://stackoverflow.com/a/6712191/236415 – Jeff Mar 26 '13 at 05:43
  • possible duplicate of [Deleting core data version xcdatamodel files](http://stackoverflow.com/questions/6712123/deleting-core-data-version-xcdatamodel-files) – Ryan McDonough Mar 26 '13 at 10:09

4 Answers4

393

It's a hack, but this worked for me:

  1. Set the Current version of the model in Xcode to one that you want to keep
  2. Remove the .xcdatamodeld from your project (Right-click -> Delete -> Remove Reference Only)
  3. Show the contents of the .xcdatamodeld package in the Finder (Right-click -> Show Package Contents)
  4. Delete the .xcdatamodel file(s) that you don't want anymore
  5. Re-add the .xcdatamodeld file to your project

This eliminates the need to manually modify any of the project metadata files.

David Avendasora
  • 4,538
  • 1
  • 16
  • 15
  • 4
    I tried this technique and found that due to the way my model versions were named, the model version were re-sorted improperly and the wrong default version was selected as I re-added the xcdatamodel file into the project. This may be because I am up to 38 model versions and I haven't changed the default version name, so the versions are named "MY_APP 37.xcdatamodel". I wanted to delete 38. On import, it was pulled in lexical order, meaning "MY_APP 10.xcdatamodel" was first, and selected as the default. Not ideal in my case. – Mike Nov 16 '12 at 05:46
  • 2
    Mike - same problem for me, though I was able to reorder in the project.pbxproj in a text editor without any major drama. Still not ideal. – Ian Kershaw Feb 13 '13 at 11:47
  • This worked for me. Maybe the deletion of a model was made intentionally uneasy, because it could lead to data loss. – berfis Nov 29 '13 at 15:53
  • 1
    This is not working with Xcode 5.1.1. Previously, with many small model changes and incremented model version numbers, lightweight auto-migration worked correctly. However, after trying to remove all the extraneous "development-only" models using this approach I get the error: "Error: Can't find or automatically infer mapping model for migration". – Dalmazio Sep 12 '14 at 21:04
  • UPDATE: This approach appears to be working. May well have been a migration-related programmer error that has since been corrected. – Dalmazio Dec 16 '14 at 06:25
  • 1
    in 6.4 there is no option to remove references. It just was deleted. The rest of the steps seemed to work – noobsmcgoobs Sep 04 '15 at 03:38
  • Just hand edit the project file to remove the references to the model version you don't want after setting the current version. – Lee Oct 12 '15 at 17:54
  • These steps seem no longer to work with Xcode 10.1. The "delete" option is disabled for core data model versions. You need to remove the model version files in the finder **AND** edit the project.pbxproj (found in the package XY.xcodeproj) and delete the reference to the files **manually**... – mstolt Jan 07 '19 at 10:41
  • 1
    This appears to work in Xcode 10.2.1. My migration was some attribute name changes so nothing major. – Gavin May 15 '19 at 14:43
42

I've just found the need to do this. I created a new model version then realised I didn't need it after all. I was surprised to find there was no way of deleting it (that I could find) from Xcode. Still, to remove it I did the following:

First I made sure it wasn't my current model version. Then I went to Finder and found my *.xcdatamodeld file. Right click and Show Package Contents on that. In there you will find the actual model file and can delete it.

This still doesn't remove it from Xcode and it will show red in the Files and Groups. To stop this from happening go back to Finder and Show Package Contents on your main *.xcodeproj file (you may want to make sure you have a backup of this first). In there open up and edit project.pbxproj do a find on your model version name, in my case "interval 8". For me it appeared on two separate lines. I removed these lines and now it's completely removed from Xcode.

Sam
  • 7,252
  • 16
  • 46
  • 65
Daniel Wood
  • 4,487
  • 3
  • 38
  • 36
  • 2
    @Daniel_Wood, yeah, I had found how to do it manually too, but watch out. It can cause some problems down the line... I had to re-do this several times, it also ends up switching the active and non-active versions around sometimes, then when you reset the correct version, it does not move it to the top of the list (sometimes) which causes more problems! Thanks so much for the input though. This seems bizarre that they would have removed this capability - we should never HAVE to go into the .xcodeproj file! – SAHM Oct 25 '11 at 15:52
14

This worked for me in XCode 10.3 without the ordering issue occurring when removing the whole model and adding it again:

  1. Select any model version other than the one you want to delete.
  2. From XCode, right-click the .xcdatamodeld file and select 'Show in Finder'.
  3. Close XCode.
  4. In finder, right-click the same file and select 'Show Package Contents'.
  5. Write down the name of the model you want to remove, then from Finder, delete it.
  6. Still in Finder, navigate to the project file (.xcodeproj).
  7. Right-click and open with Atom or any other editor.
  8. From the 'Project' pane open project.pbxproj file.
  9. Remove all lines containing the model name you removed (Example: "ABC 17.xcdatamodel"). You will find two lines similar to the following:

9D88880323C545B800A789B9 / ABC 17.xcdatamodel / = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = "ABC 17.xcdatamodel"; sourceTree = "";

and

9D88880323C545B800A789B9 / ABC 17.xcdatamodel /,

Note if you find a third line it will be something like:

currentVersion = 9D88880323C545B800A789B9 / ABC 17.xcdatamodel /;

 That means it is the currently selected model. Never delete this line. Just change the text to another model version.
  1. Re-open XCode.
Tony
  • 716
  • 9
  • 15
-5

My answer is not issue-specific at all but it's quick and works. For those using git. You can just discard unstaged changes.

git stash save --keep-index

Then you can drop that stash with a git stash drop command if you like.

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143