The most efficient plugin which can solve this problem is [Proon automatic folder pruning]. There are also two similar, more specialized plugins:
There have been a few discussions on the SquirrelMail Plugins mailing list about this, so searching the mailing list archives might be useful.