Search Unity

Question How to safely delete a prefab?

Discussion in 'Editor & General Support' started by 00christian00, Feb 7, 2022.

  1. 00christian00

    00christian00

    Joined:
    Jul 22, 2012
    Posts:
    1,035
    This may seem a dumb question, but how do I delete a prefab making sure it doesn't break anything?
    I have a project full of clutter and I am trying to clean it up.
    I took for granted that if I delete a prefab, Unity would just replace any prefab occurrence with an unpacked version but the editor says "unpack in all open scenes".
    I thought it was just a typo, but no, it did break all prefab instances on all scenes not currently open.
    So please don't tell me again I need a script to do such a basic task.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    I don't know what to tell you man. I suppose you could do it all by hand, but I'd recommend writing some code.

    https://answers.unity.com/questions/57909/find-unused-assets-in-project.html

    This is well-traveled territory. There's lots more to be read by googling for how to remove unused assets.
     
  3. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    Kurt-Dekker likes this.
  4. 00christian00

    00christian00

    Joined:
    Jul 22, 2012
    Posts:
    1,035
    I already have some script to check for usage inside scenes, but it is painful to:
    1-Open the script editor
    2-Select prefab
    3-Find where it is used.
    4-Open every scene where this is used and unpack.

    It is a serious nightmare. I was hoping there was something built in to unpack in every scene, even closed one :(
    I suppose I could modify my script to automatically open the scene and unpack, but if some bug in the script I may end up with some broken reference somewhere and don't realize it immediately and maybe only discover later when I don't even remember what that broken reference could be.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    If I really need to know these things, I prefer working from the command line and I just use
    grep
    and search the entire project for the GUID in question.

    For instance, the metafile of the CapturableBase9_hexagonal_prefab.prefab:

    Screen Shot 2022-02-08 at 6.40.06 AM.png

    And then I search that GUID using my
    grp
    utility (just a convenience wrapper around the system
    grep
    ) and it will return something like this, indicating the prefab in question is used in only one other place project-wide:

    Screen Shot 2022-02-08 at 6.41.03 AM.png

    The first hit is where the meta is, the second hit in CapturableBase9_all.asset is the ScriptableObject that contains that reference. Unity connects EVERYTHING via that GUID.

    The nice thing about grep is that it produces a huge log you can work from if you need to exhaustively change stuff to not use a particular asset, and when you're done, there will be no instances shown.
     
  6. 00christian00

    00christian00

    Joined:
    Jul 22, 2012
    Posts:
    1,035
    It is the way I use in my current script, I open the scene as a text file and look for its guid. But the problem is that it's an undoable operation and if something is wrong I may screw up the project.
    For example I discovered a bug in that script some week ago, where it wouldn't return the occurrence of an asset if it was part of a prefab or nested prefab because the scene contain only the reference to the top most asset. If I had made a deletion based on that code, I may have deleted prefab still currently used.
    That is a bug now I'm aware of and have fixed, but I cannot be aware in advance of pitfall in my code. Definitively something I wouldn't play with. This is something that should be done on the Unity side.
    If I discover the broken reference immediately, no big deal, but if it gets unnoticed it's lost forever.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Agreed. The grep approach is really only to do that one final verification that something isn't being used at all, which I kind of assumed was the original intent of your "can I delete this" initial post.

    Assuming text serialization of assets, the default setting in Unity now, if you do not see a GUID in the project, I believe that is a 100% guarantee it isn't directly referenced by Unity.

    Don't forget that stuff under a folder named Resources/ could always be loaded by filename! And same goes for stuff in the StreamingAssets folder