Searching for orphaned DB files on the SQL server

  • Martin Tydor

    SSC Enthusiast

    Points: 176

    Comments posted to this topic are about the item Searching for orphaned DB files on the SQL server

    M.T.

  • Jeff Moden

    SSC Guru

    Points: 997308

    Any MDF, NDF, or LDF file currently being used by SQL Server cannot be modified in any way.  With that in mind (and it would also work if multiple instances had files in a given directory), why not just tell the system to rename them all with a (for example), "NU_" prefix skipping the ones that cannot be renamed?  Then, to find all the files that could be moved to an archive or deleted or whatever, just do a DIR "NU_*.*" in the directory and you'll have your list.  Same could be done with a Move or Delete command.

    The REN command is self-recursive and doesn't stop when it finds a file that is currently in use.  For example...

    REN *.MDF NU_*.MDF

    Only the inactive files will be renamed, if any.

     

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".
    "Dear Lord... I'm a DBA so please give me patience because, if you give me strength, I'm going to need bail money too!"

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Martin Tydor

    SSC Enthusiast

    Points: 176

    Jeff, you would be right but if DB is OFFLINE, you are able e.g. delete db file. I am pretty sure you would be able to rename files from other instance if some DB is offline.

    M.T.

  • sterling3721

    SSC-Addicted

    Points: 471

    Yes, offline database data files can be deleted, I believe that's the top 1 reason why a data file was orphaned, drop an offline database will leave the data files around.

  • Jeff Moden

    SSC Guru

    Points: 997308

    True enough.  Thanks for the feedback.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".
    "Dear Lord... I'm a DBA so please give me patience because, if you give me strength, I'm going to need bail money too!"

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply