{"id":831,"date":"2017-04-26T07:00:42","date_gmt":"2017-04-26T14:00:42","guid":{"rendered":"http:\/\/3.209.169.194\/blogs\/erin\/?p=831"},"modified":"2020-04-21T09:55:29","modified_gmt":"2020-04-21T16:55:29","slug":"remove-files-from-tempdb","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/erin\/remove-files-from-tempdb\/","title":{"rendered":"Remove Files From tempdb"},"content":{"rendered":"<p>I made a mistake with a script today. I created three new tempdb files sized at 10GB each that filled up a hard drive.<\/p>\n<p>Whoops.<\/p>\n<p>Luckily it was in one of my own testing VMs, so it wasn&#8217;t awful. Fixing it, however, was a fun one.<\/p>\n<blockquote><p>**NOTE: All work was done in a test environment. Proceed with caution if you&#8217;re running these commands in Production and make sure you understand the ramifications.<\/p><\/blockquote>\n<p>In order to remove a file from a database in SQL Server, it has to be empty. For each file I wanted to remove I needed to run:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nUSE &#x5B;tempdb];\r\nGO\r\nDBCC SHRINKFILE (logicalname, EMPTYFILE);\r\nGO\r\n<\/pre>\n<p>However, every time I tried to run this command for any file, I would get a message like this:<\/p>\n<p>DBCC SHRINKFILE: Page 4:130 could not be moved because it is a work table page.<br \/>\nMsg 2555, Level 16, State 1, Line 1<br \/>\nCannot move all contents of file &#8220;logicalname&#8221; to other places to complete the emptyfile operation.<\/p>\n<p>This error came up for each file, even if I restarted the instance and did nothing, and even if I restarted it in single-user mode.<\/p>\n<p>Then I found some posts about clearing the procedure cache, and the session cache, so I cleared everything&#8230;go big or go home right? Remember, I&#8217;m working in a local test environment so this isn&#8217;t a big deal.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nDBCC DROPCLEANBUFFERS\r\nGO\r\nDBCC FREEPROCCACHE\r\nGO\r\nDBCC FREESESSIONCACHE\r\nGO\r\nDBCC FREESYSTEMCACHE ( 'ALL')\r\nGO\r\n<\/pre>\n<p>If I tried to empty the file after that, it still failed.<\/p>\n<p>**Note: In talking with Jonathan after the fact, he said he&#8217;s seen this before, where every file in tempdb has a workfile in it that you cannot remove. He thinks the behavior started with SQL Server 2012. I haven&#8217;t found any documentation from Microsoft about this&#8230;yet&#8230;<\/p>\n<p>Now I was getting annoyed (mostly with myself for this mistake in the first place). Finally, I tried started SQL Server with <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/database-engine\/configure-windows\/start-sql-server-with-minimal-configuration\">minimal configuration<\/a>, using -f, and connected with <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/relational-databases\/scripting\/sqlcmd-use-the-utility\" class=\"broken_link\">sqlcmd<\/a>. The documentation notes that &#8220;tempdb is configured at the smallest possible size.&#8221; So small that not all the files were there! I couldn&#8217;t run the DBCC SHRINKFILE command because the additional files weren&#8217;t available. Perfect, as then I could just remove them:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nALTER DATABASE &#x5B;tempdb]  REMOVE FILE &#x5B;logicalname]\r\nGO\r\n<\/pre>\n<p>I ran the ALTER DATABASE [tempdb] REMOVE FILE for each of the three files I added, shut down the instance, removed -f, and restarted. The files were removed! However, they were still sitting out on the drive, but because they were no longer in use I could delete them. Space reclaimed, time for some chocolate.<\/p>\n<p><em>Update April 21, 2020: <a href=\"https:\/\/twitter.com\/AMtwo\">Andy Mallon<\/a> has a clever alternative to starting SQL Server with -f, check out his post <a href=\"https:\/\/am2.co\/2020\/04\/fixing-tempdb\/\">Fixing tempdb: Growing, shrinking, and removing data files<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I made a mistake with a script today. I created three new tempdb files sized at 10GB each that filled up a hard drive. Whoops. Luckily it was in one of my own testing VMs, so it wasn&#8217;t awful. Fixing it, however, was a fun one. **NOTE: All work was done in a test environment. [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Remove Files From tempdb - Erin Stellato<\/title>\n<meta name=\"description\" content=\"The need to remove files from tempdb probably doesn&#039;t come up frequently, but due to the nature of tempdb, it&#039;s not always an easy thing to do.\" \/>\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.sqlskills.com\/blogs\/erin\/remove-files-from-tempdb\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Remove Files From tempdb - Erin Stellato\" \/>\n<meta property=\"og:description\" content=\"The need to remove files from tempdb probably doesn&#039;t come up frequently, but due to the nature of tempdb, it&#039;s not always an easy thing to do.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/erin\/remove-files-from-tempdb\/\" \/>\n<meta property=\"og:site_name\" content=\"Erin Stellato\" \/>\n<meta property=\"article:published_time\" content=\"2017-04-26T14:00:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-04-21T16:55:29+00:00\" \/>\n<meta name=\"author\" content=\"Erin Stellato\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Erin Stellato\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. 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\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/remove-files-from-tempdb\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/remove-files-from-tempdb\/\",\"name\":\"Remove Files From tempdb - Erin Stellato\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/#website\"},\"datePublished\":\"2017-04-26T14:00:42+00:00\",\"dateModified\":\"2020-04-21T16:55:29+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/76170223ffffa1df03fd9be5b66cb158\"},\"description\":\"The need to remove files from tempdb probably doesn't come up frequently, but due to the nature of tempdb, it's not always an easy thing to do.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/remove-files-from-tempdb\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/erin\/remove-files-from-tempdb\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/remove-files-from-tempdb\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Remove Files From tempdb\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/\",\"name\":\"Erin Stellato\",\"description\":\"The SQL Sequel\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/76170223ffffa1df03fd9be5b66cb158\",\"name\":\"Erin Stellato\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0c8b485bd54ea26b57e99f79b525f409?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0c8b485bd54ea26b57e99f79b525f409?s=96&d=mm&r=g\",\"caption\":\"Erin Stellato\"},\"sameAs\":[\"http:\/\/3.209.169.194\/blogs\/erin\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/author\/erin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Remove Files From tempdb - Erin Stellato","description":"The need to remove files from tempdb probably doesn't come up frequently, but due to the nature of tempdb, it's not always an easy thing to do.","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.sqlskills.com\/blogs\/erin\/remove-files-from-tempdb\/","og_locale":"en_US","og_type":"article","og_title":"Remove Files From tempdb - Erin Stellato","og_description":"The need to remove files from tempdb probably doesn't come up frequently, but due to the nature of tempdb, it's not always an easy thing to do.","og_url":"https:\/\/www.sqlskills.com\/blogs\/erin\/remove-files-from-tempdb\/","og_site_name":"Erin Stellato","article_published_time":"2017-04-26T14:00:42+00:00","article_modified_time":"2020-04-21T16:55:29+00:00","author":"Erin Stellato","twitter_misc":{"Written by":"Erin Stellato","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/remove-files-from-tempdb\/","url":"https:\/\/www.sqlskills.com\/blogs\/erin\/remove-files-from-tempdb\/","name":"Remove Files From tempdb - Erin Stellato","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/#website"},"datePublished":"2017-04-26T14:00:42+00:00","dateModified":"2020-04-21T16:55:29+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/76170223ffffa1df03fd9be5b66cb158"},"description":"The need to remove files from tempdb probably doesn't come up frequently, but due to the nature of tempdb, it's not always an easy thing to do.","breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/remove-files-from-tempdb\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/erin\/remove-files-from-tempdb\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/remove-files-from-tempdb\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/erin\/"},{"@type":"ListItem","position":2,"name":"Remove Files From tempdb"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/erin\/","name":"Erin Stellato","description":"The SQL Sequel","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/erin\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/76170223ffffa1df03fd9be5b66cb158","name":"Erin Stellato","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0c8b485bd54ea26b57e99f79b525f409?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0c8b485bd54ea26b57e99f79b525f409?s=96&d=mm&r=g","caption":"Erin Stellato"},"sameAs":["http:\/\/3.209.169.194\/blogs\/erin"],"url":"https:\/\/www.sqlskills.com\/blogs\/erin\/author\/erin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/posts\/831"}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/comments?post=831"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/posts\/831\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/media?parent=831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/categories?post=831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/tags?post=831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}