Invoke-DbaDbShrink: let files be shrunk evenly and say why they don't shrink
Summarize Functionality
I often use Invoke-DbaDbShrink for tempdb, but:
- I am not aware of any way to get
Invoke-DbaDbShrinkto shrink files evenly - Unlike the T-SQL that
Invoke-DbaDbShrinkcalls,Invoke-DbaDbShrinkdoes not print out why my shrink has failed (which often happens in tempdb)
Both of these would help a lot for tempdb operations, which are just about the only sensible example of shrinking a database.
Is there a command that is similiar or close to what you are looking for?
Yes
Technical Details
I'm open to the idea of this being a new command. Shrinking tempdb is a common enough operation on small dev servers that I would love to either have a super easy way to do it or at least an example in the documentation that I can just copy and paste.
try Invoke-DbaBalanceDataFiles after performing a shrink. seems thats what ur looking for.
@claude can you make it output the error message?
Claude finished @potatoqualitee's task —— View job • claude/issue-10033-20251202-1615 • Create PR ➔
Completed: Error message output for Invoke-DbaDbShrink
Task List:
- [x] Read CLAUDE.md for repo guidelines
- [x] Locate and read Invoke-DbaDbShrink command file
- [x] Understand current T-SQL execution and error handling
- [x] Implement error message capture and output
- [x] Tests not updated (no parameter changes)
- [x] Commit and push changes
- [x] Provide PR link
Summary
I've enhanced Invoke-DbaDbShrink to output detailed error messages when shrink operations fail or don't achieve their target size. This addresses the issue raised about tempdb shrink operations that commonly fail or only partially complete.
Changes Made
File modified: public/Invoke-DbaDbShrink.ps1
-
Enhanced error message capture (lines 298-301): Added detailed exception message capture including inner exceptions when shrink operations fail
-
Warning output (line 302): Now displays warning messages via
Write-Messagewhen shrink fails, ensuring users see the error in console output -
Partial shrink detection (lines 315-322): Added logic to detect when a file didn't shrink to the target size (common in tempdb with active transactions) and provides informative feedback about the shortfall and possible reasons
-
Error details in output (lines 337-340, 362): Error messages are now included in the
Notesproperty of the output object, making them visible in the results table
Example Output
When a shrink fails or only partially completes, users will now see:
- Warning messages in the console with detailed error information
- The error details in the
Notescolumn of the output object - Clear indication of the shortfall between target and actual final size
This is particularly useful for tempdb operations where shrink often fails due to active transactions or data distribution.
Branch and PR
--- |
@potatoqualitee Invoke-DbaBalanceDataFiles looks to be more for index rebuilds than anything else. Also, isn't that more work than it needs to be? If I want to save 8 GB in tempdb, then I would rather shrink 8 files by 1 GB each than make a mistake and then correct it with Invoke-DbaBalanceDataFiles.