{"id":734,"date":"2022-12-05T23:48:39","date_gmt":"2022-12-05T23:48:39","guid":{"rendered":"https:\/\/www.naveedulhaq.com\/?p=734"},"modified":"2022-12-24T22:08:31","modified_gmt":"2022-12-24T22:08:31","slug":"move-sql-server-mdf-and-ldf-files-location","status":"publish","type":"post","link":"https:\/\/www.naveedulhaq.com\/index.php\/sql\/move-sql-server-mdf-and-ldf-files-location\/","title":{"rendered":"Move SQL Server MDF and LDF Files location"},"content":{"rendered":"\n<p>A lot of times we come across issues when our disk space is consumed and we need to move our database MDF &amp; LDF files from their default location. <\/p>\n\n\n\n<p>In an earlier version of SQL Server, you have to use Detach and Attach method in order to move database files location. However, it is an old method and you do not have to follow the same method now.<\/p>\n\n\n\n<p>I use the following steps to move database MDF and LDF files from one location to another.<\/p>\n\n\n\n<p>Let&#8217;s assume the current location of MDF and LDF files are in the <strong>C drive<\/strong> and you want to move these files to the <strong>D drive<\/strong>.<\/p>\n\n\n\n<p>Open SQL Management Studio and open the query window and follow the below steps. replace &#8220;YOURDATABASENAME &#8221; with the name of the actual database.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Get Current Location<\/h2>\n\n\n\n<p>use the following SQL to get the current location of database files<\/p>\n\n\n\n<pre><code>SELECT name, physical_name AS CurrentLocation, state_desc\nFROM sys.master_files\nWHERE database_id = DB_ID(N'YOURDATABASENAME ');<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Offline Database<\/h2>\n\n\n\n<pre><code>ALTER DATABASE YOURDATABASENAME SET OFFLINE;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Move MDF &amp; LDF files<\/h2>\n\n\n\n<p>Since the database is now offline, you can move MDF and LDF files to a new location with the xCopy command in the command prompt or manually copy\/paste using explorer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Update File location in the database<\/h2>\n\n\n\n<p>You can use the following command to update MDF and LDF file location in the database<\/p>\n\n\n\n<pre><code>ALTER DATABASE YOURDATABASENAME \nMODIFY FILE ( NAME = YOURDATABASENAME_Data, FILENAME = 'D:\\data\\YOURDATABASENAME.mdf' );\nALTER DATABASE YOURDATABASENAME \nMODIFY FILE ( NAME = YOURDATABASENAME_Logs, FILENAME = 'D:\\data\\YOURDATABASENAME.ldf' );\nGO<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Online Database<\/h2>\n\n\n\n<pre><code>ALTER DATABASE YOURDATABASENAMESET ONLINE<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Verify<\/h2>\n\n\n\n<pre><code>SELECT name, physical_name AS CurrentLocation, state_desc\nFROM sys.master_files\nWHERE database_id = DB_ID(N'YOURDATABASENAME ');<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A lot of times we come across issues when our disk space is consumed and we need to move our database MDF &amp; LDF files&#8230;<\/p>\n","protected":false},"author":1,"featured_media":663,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[84],"tags":[32,23,17],"class_list":["post-734","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sql","tag-developer","tag-development","tag-sql-server"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Move SQL Server MDF and LDF Files location - Naveed Ul-Haq&#039;s blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.naveedulhaq.com\/index.php\/sql\/move-sql-server-mdf-and-ldf-files-location\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Move SQL Server MDF and LDF Files location - Naveed Ul-Haq&#039;s blog\" \/>\n<meta property=\"og:description\" content=\"A lot of times we come across issues when our disk space is consumed and we need to move our database MDF &amp; LDF files...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.naveedulhaq.com\/index.php\/sql\/move-sql-server-mdf-and-ldf-files-location\/\" \/>\n<meta property=\"og:site_name\" content=\"Naveed Ul-Haq&#039;s blog\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-05T23:48:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-12-24T22:08:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.naveedulhaq.com\/wp-content\/uploads\/2022\/05\/sql.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1551\" \/>\n\t<meta property=\"og:image:height\" content=\"1516\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Naveed Ul-Haq\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Naveed Ul-Haq\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/sql\\\/move-sql-server-mdf-and-ldf-files-location\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/sql\\\/move-sql-server-mdf-and-ldf-files-location\\\/\"},\"author\":{\"name\":\"Naveed Ul-Haq\",\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/#\\\/schema\\\/person\\\/dd6db5980b965fcae41e096d357c65c9\"},\"headline\":\"Move SQL Server MDF and LDF Files location\",\"datePublished\":\"2022-12-05T23:48:39+00:00\",\"dateModified\":\"2022-12-24T22:08:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/sql\\\/move-sql-server-mdf-and-ldf-files-location\\\/\"},\"wordCount\":221,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/#\\\/schema\\\/person\\\/dd6db5980b965fcae41e096d357c65c9\"},\"image\":{\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/sql\\\/move-sql-server-mdf-and-ldf-files-location\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.naveedulhaq.com\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/sql.jpg\",\"keywords\":[\"developer\",\"development\",\"Sql Server\"],\"articleSection\":[\"Sql\"],\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/sql\\\/move-sql-server-mdf-and-ldf-files-location\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/sql\\\/move-sql-server-mdf-and-ldf-files-location\\\/\",\"url\":\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/sql\\\/move-sql-server-mdf-and-ldf-files-location\\\/\",\"name\":\"Move SQL Server MDF and LDF Files location - Naveed Ul-Haq&#039;s blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/sql\\\/move-sql-server-mdf-and-ldf-files-location\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/sql\\\/move-sql-server-mdf-and-ldf-files-location\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.naveedulhaq.com\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/sql.jpg\",\"datePublished\":\"2022-12-05T23:48:39+00:00\",\"dateModified\":\"2022-12-24T22:08:31+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/sql\\\/move-sql-server-mdf-and-ldf-files-location\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/sql\\\/move-sql-server-mdf-and-ldf-files-location\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/sql\\\/move-sql-server-mdf-and-ldf-files-location\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.naveedulhaq.com\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/sql.jpg\",\"contentUrl\":\"https:\\\/\\\/www.naveedulhaq.com\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/sql.jpg\",\"width\":1551,\"height\":1516,\"caption\":\"SQL Server\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/sql\\\/move-sql-server-mdf-and-ldf-files-location\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.naveedulhaq.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Move SQL Server MDF and LDF Files location\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/#website\",\"url\":\"https:\\\/\\\/www.naveedulhaq.com\\\/\",\"name\":\"Naveed Ul-Haq's blog\",\"description\":\"AI, Optimizely, Azure &amp; more\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/#\\\/schema\\\/person\\\/dd6db5980b965fcae41e096d357c65c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.naveedulhaq.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/#\\\/schema\\\/person\\\/dd6db5980b965fcae41e096d357c65c9\",\"name\":\"Naveed Ul-Haq\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/362536aba6cc66917d7558cacd015a81c7cdf1a69b9a28c994764847c487b692?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/362536aba6cc66917d7558cacd015a81c7cdf1a69b9a28c994764847c487b692?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/362536aba6cc66917d7558cacd015a81c7cdf1a69b9a28c994764847c487b692?s=96&d=mm&r=g\",\"caption\":\"Naveed Ul-Haq\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/362536aba6cc66917d7558cacd015a81c7cdf1a69b9a28c994764847c487b692?s=96&d=mm&r=g\"},\"description\":\"I lead engineering delivery teams for digital commerce and digital experience platforms, combining hands-on architecture experience with strong delivery governance. Over 20+ years, I\u2019ve built and led cross\u2011functional teams (engineering, BA, QA) delivering modern cloud solutions on Azure, microservices, APIs and eCommerce\\\/CMS platforms. My recent focus includes AI-enabled commerce automation: product ingestion and enrichment workflows, content optimisation for SEO and shopping feeds, and agentic tooling to improve marketing and accessibility workflows. I\u2019m passionate about building high-performing teams, establishing quality\\\/release standards, and delivering measurable outcomes across performance, reliability, and speed of change.\",\"sameAs\":[\"https:\\\/\\\/www.naveedulhaq.com\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/naveedulhaq\\\/\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Move SQL Server MDF and LDF Files location - Naveed Ul-Haq&#039;s blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.naveedulhaq.com\/index.php\/sql\/move-sql-server-mdf-and-ldf-files-location\/","og_locale":"en_GB","og_type":"article","og_title":"Move SQL Server MDF and LDF Files location - Naveed Ul-Haq&#039;s blog","og_description":"A lot of times we come across issues when our disk space is consumed and we need to move our database MDF &amp; LDF files...","og_url":"https:\/\/www.naveedulhaq.com\/index.php\/sql\/move-sql-server-mdf-and-ldf-files-location\/","og_site_name":"Naveed Ul-Haq&#039;s blog","article_published_time":"2022-12-05T23:48:39+00:00","article_modified_time":"2022-12-24T22:08:31+00:00","og_image":[{"width":1551,"height":1516,"url":"https:\/\/www.naveedulhaq.com\/wp-content\/uploads\/2022\/05\/sql.jpg","type":"image\/jpeg"}],"author":"Naveed Ul-Haq","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Naveed Ul-Haq","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.naveedulhaq.com\/index.php\/sql\/move-sql-server-mdf-and-ldf-files-location\/#article","isPartOf":{"@id":"https:\/\/www.naveedulhaq.com\/index.php\/sql\/move-sql-server-mdf-and-ldf-files-location\/"},"author":{"name":"Naveed Ul-Haq","@id":"https:\/\/www.naveedulhaq.com\/#\/schema\/person\/dd6db5980b965fcae41e096d357c65c9"},"headline":"Move SQL Server MDF and LDF Files location","datePublished":"2022-12-05T23:48:39+00:00","dateModified":"2022-12-24T22:08:31+00:00","mainEntityOfPage":{"@id":"https:\/\/www.naveedulhaq.com\/index.php\/sql\/move-sql-server-mdf-and-ldf-files-location\/"},"wordCount":221,"commentCount":0,"publisher":{"@id":"https:\/\/www.naveedulhaq.com\/#\/schema\/person\/dd6db5980b965fcae41e096d357c65c9"},"image":{"@id":"https:\/\/www.naveedulhaq.com\/index.php\/sql\/move-sql-server-mdf-and-ldf-files-location\/#primaryimage"},"thumbnailUrl":"https:\/\/www.naveedulhaq.com\/wp-content\/uploads\/2022\/05\/sql.jpg","keywords":["developer","development","Sql Server"],"articleSection":["Sql"],"inLanguage":"en-GB","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.naveedulhaq.com\/index.php\/sql\/move-sql-server-mdf-and-ldf-files-location\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.naveedulhaq.com\/index.php\/sql\/move-sql-server-mdf-and-ldf-files-location\/","url":"https:\/\/www.naveedulhaq.com\/index.php\/sql\/move-sql-server-mdf-and-ldf-files-location\/","name":"Move SQL Server MDF and LDF Files location - Naveed Ul-Haq&#039;s blog","isPartOf":{"@id":"https:\/\/www.naveedulhaq.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.naveedulhaq.com\/index.php\/sql\/move-sql-server-mdf-and-ldf-files-location\/#primaryimage"},"image":{"@id":"https:\/\/www.naveedulhaq.com\/index.php\/sql\/move-sql-server-mdf-and-ldf-files-location\/#primaryimage"},"thumbnailUrl":"https:\/\/www.naveedulhaq.com\/wp-content\/uploads\/2022\/05\/sql.jpg","datePublished":"2022-12-05T23:48:39+00:00","dateModified":"2022-12-24T22:08:31+00:00","breadcrumb":{"@id":"https:\/\/www.naveedulhaq.com\/index.php\/sql\/move-sql-server-mdf-and-ldf-files-location\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.naveedulhaq.com\/index.php\/sql\/move-sql-server-mdf-and-ldf-files-location\/"]}]},{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/www.naveedulhaq.com\/index.php\/sql\/move-sql-server-mdf-and-ldf-files-location\/#primaryimage","url":"https:\/\/www.naveedulhaq.com\/wp-content\/uploads\/2022\/05\/sql.jpg","contentUrl":"https:\/\/www.naveedulhaq.com\/wp-content\/uploads\/2022\/05\/sql.jpg","width":1551,"height":1516,"caption":"SQL Server"},{"@type":"BreadcrumbList","@id":"https:\/\/www.naveedulhaq.com\/index.php\/sql\/move-sql-server-mdf-and-ldf-files-location\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.naveedulhaq.com\/"},{"@type":"ListItem","position":2,"name":"Move SQL Server MDF and LDF Files location"}]},{"@type":"WebSite","@id":"https:\/\/www.naveedulhaq.com\/#website","url":"https:\/\/www.naveedulhaq.com\/","name":"Naveed Ul-Haq's blog","description":"AI, Optimizely, Azure &amp; more","publisher":{"@id":"https:\/\/www.naveedulhaq.com\/#\/schema\/person\/dd6db5980b965fcae41e096d357c65c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.naveedulhaq.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":["Person","Organization"],"@id":"https:\/\/www.naveedulhaq.com\/#\/schema\/person\/dd6db5980b965fcae41e096d357c65c9","name":"Naveed Ul-Haq","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/362536aba6cc66917d7558cacd015a81c7cdf1a69b9a28c994764847c487b692?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/362536aba6cc66917d7558cacd015a81c7cdf1a69b9a28c994764847c487b692?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/362536aba6cc66917d7558cacd015a81c7cdf1a69b9a28c994764847c487b692?s=96&d=mm&r=g","caption":"Naveed Ul-Haq"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/362536aba6cc66917d7558cacd015a81c7cdf1a69b9a28c994764847c487b692?s=96&d=mm&r=g"},"description":"I lead engineering delivery teams for digital commerce and digital experience platforms, combining hands-on architecture experience with strong delivery governance. Over 20+ years, I\u2019ve built and led cross\u2011functional teams (engineering, BA, QA) delivering modern cloud solutions on Azure, microservices, APIs and eCommerce\/CMS platforms. My recent focus includes AI-enabled commerce automation: product ingestion and enrichment workflows, content optimisation for SEO and shopping feeds, and agentic tooling to improve marketing and accessibility workflows. I\u2019m passionate about building high-performing teams, establishing quality\/release standards, and delivering measurable outcomes across performance, reliability, and speed of change.","sameAs":["https:\/\/www.naveedulhaq.com","https:\/\/www.linkedin.com\/in\/naveedulhaq\/"]}]}},"_links":{"self":[{"href":"https:\/\/www.naveedulhaq.com\/index.php\/wp-json\/wp\/v2\/posts\/734","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.naveedulhaq.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.naveedulhaq.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.naveedulhaq.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.naveedulhaq.com\/index.php\/wp-json\/wp\/v2\/comments?post=734"}],"version-history":[{"count":0,"href":"https:\/\/www.naveedulhaq.com\/index.php\/wp-json\/wp\/v2\/posts\/734\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.naveedulhaq.com\/index.php\/wp-json\/wp\/v2\/media\/663"}],"wp:attachment":[{"href":"https:\/\/www.naveedulhaq.com\/index.php\/wp-json\/wp\/v2\/media?parent=734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.naveedulhaq.com\/index.php\/wp-json\/wp\/v2\/categories?post=734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.naveedulhaq.com\/index.php\/wp-json\/wp\/v2\/tags?post=734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}