0

I am unable to delete a filegroup. I am getting the standard error: "The filegroup '~~~~' cannot be removed because it is not empty." I have been searching the internet, and still cannot find the answer. The server version is Microsoft SQL Server 2019.

I have gone through both of these very thoroughly:

Things that I have scanned to see if there is any association to the filegroup:

  • files : 0
  • allocation_units : 0
  • indexes : 0
  • partition schemes : 0
  • partition functions : 0
  • partitions : 0

I have also run DBCC CHECKFILEGROUP and it shows nothing as well. I am really very confused. Can someone help me figure out what else could be the thing keeping me from dropping this filegroup?

Full disclosure, I am using a module I wrote:

https://github.com/tcartwright/tcdbtools/blob/main/docs/Invoke-DBSafeShrink.md

The module performs the following steps:

  • creates a temporary file and filegroup
  • moves clustered indexes, non-clustered indexes, LOBS, and heaps to the new filegroup
  • shrinks the original filegroup
  • moves all of the original objects back to the origin filegroup
  • removes the temporary file group

As part of moving the LOBs I am using this trick from Kimberly Tripp to move them: https://www.sqlskills.com/blogs/kimberly/understanding-lob-data-20082008r2-2012/

EDIT: I think I have figured out what is blocking the drop, but I cannot figure out how to fix it. This query:

SELECT * FROM sys.[tables] AS [t] WHERE [t].[lob_data_space_id] IN (
    SELECT [ds].[data_space_id] FROM sys.[data_spaces] AS [ds] WHERE [ds].[name] <> 'PRIMARY'
)

returns a table that does not have any associated LOB data in the allocation units. Nor can I figure out how to get rid of this. I have:

  • dropped and recreated the PK for this table
  • rebuilt all of the indexes on the table and

None of which helped.

8
  • Are you using table partitioning?
    – Dan Guzman
    Commented Jun 9, 2023 at 22:30
  • @DanGuzman No, no table partitioning. Commented Jun 9, 2023 at 23:49
  • FWIW, it's kind of pointless and wasteful to arbitrarily shrink a database and rebuild indexes. Don't get me wrong, it's cool you're creating a thing, but I'd habit a guess you're battling the wrong problems.
    – J.D.
    Commented Jun 10, 2023 at 0:31
  • @J.D. rebuilding them is part of the movement process. I don't actually issue a rebuild statement. Of course you could argue whether or not this is a good idea with Paul Randal who I took the idea from. :) Commented Jun 10, 2023 at 0:36
  • I understand, but rebuilding is an unnecessary action is my point, regardless of how you do it. Some people will use your scripts with the purpose of rebuilding. Paul states, in the same article, to avoid doing all of this, especially arbitrarily - so there's nothing to argue: "Bottom line – try to avoid running data-file shrink at all costs!" - Paul.
    – J.D.
    Commented Jun 10, 2023 at 0:46

2 Answers 2

0

I think there is an objects in that file group for sure, so, you may first want to know what it is, this script may help

after that changing location or dropping those objects will let you drop the FG

1
  • Ty for the script Sina, but sadly its very similar to ones I have been running. I ran it anyway, and it produced 0 rows for the file group I am wanting to remove. Commented Jun 10, 2023 at 18:45
0

Ok, I finally figured it out. Before I go into the fix, let me theorize on what I think might have happened. I am using the partition trick to move LOB data from one FG to another. Combined with the fact that the part of the move process using CREATE INDEX (DROP_EXISTING = ON) causes the index to rebuild.

I think when the index rebuilt on the new FG that possibly all of the LOB data moved to in row eliminating the LOB_DATA allocation unit. However, the lob_dataspace_id remained set to the new FG after the move. Causing me not to be able to delete it.

When the code went to move it back, it did not see any LOB_DATA allocated, and did not use the partition trick to move the index back.

To fix this, I needed to move the index back to PRIMARY using the same partition trick.

I modified the code to also inspect the tables lob_dataspace_id to determine if the FG I am moving to is not the same. Then it will utilize the partition trick to move the index back, and caused the lob_dataspace_id to reset.

Once I moved the index back and forth to PRIMARY using the partition trick I was able to successfully delete the filegroup.

3
  • Ok, it turns out the rebuild did not cause it. I have several clustered indexes in various databases that do not have LOB_DATA, but they have a non zero lob_dataspace_id. No idea why. The fix remains the same. I have to use the partition trick to move these indexes. Commented Jun 10, 2023 at 21:55
  • CREATE TABLE command includes TEXTIMAGE_ON section. It is not a part of CREATE INDEX nor ALTER INDEX...REBUILD commands so can be changed using the partition trick or another CREATE TABLE command. Commented Jun 12, 2023 at 9:00
  • @DenisRubashkin Denis, the table in question does not have that option set when I generate its create script. The options it has: WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] Commented Jun 12, 2023 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.