Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8450557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:59:59+00:00 2026-06-10T10:59:59+00:00

I use SQL Server 2005 Express user instances for unit testing. Each test randomly

  • 0

I use SQL Server 2005 Express user instances for unit testing. Each test randomly generates a database name, so the connection string looks like this:

Data Source=.\SQLEXPRESS2005;Database=MyTest_624332256;User Instance=true;Integrated Security=SSPI;

where 624332256 is a randomly generated number.

At the end I want to delete all those database files (.MDF and .LDF).

If I call sp_detach_db or even DROP DATABASE it deletes the DB instances from sys.databases, however the database files still remain in %LOCALAPPDATA%\Microsoft\Microsoft SQL Server Data\SQLEXPRESS2005

How do I delete the files, too? I thought DROP DATABASE was supposed to do this, but it doesn’t!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:00:01+00:00Added an answer on June 10, 2026 at 11:00 am

    I ended up writing code to do this manually, querying for the physical file names:

    public static void CleanUpAllTestDbs()
    {
            using (var connection = new SqlConnection(connString))
            {
                connection.Open();
    
                var dbNames = new List<string>();
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = @"
    USE master
    
    SELECT name FROM sysdatabases WHERE name LIKE 'MyTest_%'";
    
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            dbNames.Add(reader.GetString(0));
                        }
                    }
                }
    
                foreach (var dbName in dbNames)
                {
                    GetDatabaseFiles(connection, dbName, databaseFiles);
                    DetachDatabase(connection, dbName);
                }
            }
    
            // Delete the physical files
    
            foreach (var filePath in databaseFiles)
            {
                File.Delete(filePath);
            }
    }
    
    private static void DetachDatabase(DbConnection connection, string dbName)
    {
        using (var command = connection.CreateCommand())
        {
            command.CommandText = string.Format(@"
    USE master
    
    ALTER DATABASE [{0}] SET OFFLINE WITH ROLLBACK IMMEDIATE
    DROP DATABASE [{0}]", dbName);
    
            try
            {
                command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                if (!ex.Message.StartsWith("Unable to open the physical file", StringComparison.OrdinalIgnoreCase))
                    throw;
            }
        }
    }
    
    private static void GetDatabaseFiles(DbConnection connection, string dbName, List<string> results)
    {
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT physical_name FROM [" + dbName + "].sys.database_files";
    
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    results.Add(reader.GetString(0));
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can we use SSIS with SQL Server (2005) Express as database?
Our program ships with an SQL Server 2005 database and SQL Server 2005 Express.
Is it possible to use SQL Dependency with SQL Server Express 2005 which comes
I use SQL Server 2005, I have a database which uses full-text catalog. I
I have converted a jet database to a sql server 2005 express database. The
I have a somewhat small database in SQL Server Express 2005 that I really
I am trying to restore a database in my sql server 2005 express edition.
I am using SQL Server Express 2005. I have a single database myDB I
I am using SQL Server Express 2005. I have only a single database to
i use sql server 2008 for publisher and sql server 2005 express for subscriber

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.