Searching for orphaned DB files on the SQL server

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

    M.T.

  • 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.


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

  • 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.

  • 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.

  • 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.


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

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

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