Forcing MSMQ to clean up its storage files

As I've probably discussed before, MSMQ does not perform garbage collection on its message storage files very often - every 6 hours or on service startup are the defaults.

You can, though, run a script to ask the MSMQ service not to wait around. This is done by calling the Tidy method of the MSMQApplication object.

[[Thanks to Frank Boyne for posting this on the MSMQ performance newsgroup]]

Option Explicit
Dim mqapp
set mqapp = WScript.CreateObject("MSMQ.MSMQApplication")
WScript.Echo "Bytes in all queues: " + CStr(mqapp.BytesInAllQueues)
mqapp.Tidy
WScript.Echo "MSMQ cleaned up"

As MSDN says: 

This method cleans up empty message files, regardless of the automatic cleanup interval used by Message Queuing. Message Queuing automatically cleans up empty message files based on its default six hour cleanup interval or based on the MessageCleanupInterval registry value.

This method (by default) applies to the local computer. To reference another computer, set MSMQApplication.Machine to another computer and call this method again.

Local administrative permissions are needed to clean up the empty message files on a computer. 

Note - only truly empty storage files are removed - one lonely message will keep a storage file active as MSMQ performs no defragmentation or compaction.