Digging occurs for many reasons. Dogs dig to bury and recover bones; they dig cooling pits when it's hot and warming pits when it's cold; they dig up prey and they dig dens. Digging is a highly enjoyable and natural canine activity. Lack of exercise, prolonged confinement and boredom can also cause digging. Once digging starts, it can quickly become a habit.
If you provide your dog with regular walks, runs, play-time and training, the chances are you will not have a digging problem. But until your dog has been taught that digging up your garden is unacceptable, he should not be given free access to these areas when you are not there to supervise him. Temporarily confine your dog indoors or to a dog run. Confinement is not the solution; it is only a temporary measure until your dog can be trusted not to destroy your garden and yard.
If your dog loves digging, provide him with his own digging pit just as parents would provide their child with a sand box. Make sure the pit is neither directly in the summer sun nor unprotected against the winter winds. A 3 x 6 foot area about 2 feet deep is sufficient. Dig it up to loosen the dirt and mix in a little sand to help it drain in the rainy season. Let your dog watch the preparations and if he joins in to help, lavish him with praise. Once the pit is ready, it is easy to get your dog digging in it. Take some of his favorite toys and treats and let him watch you make a fuss over burying them. Call your dog over and help him dig things up. Once he gets the idea and is digging without your help, enthusiastically praise him and repeat, 'Dig in your pit, dig in your pit.' When he digs up a treat, he is immediately rewarded by getting to eat it. If it's a ball or toy you've buried then you can immediately play a short game of fetch, then bury it again. Repeat this over and over, always repeating the command, 'Dig in your pit.' Your dog will quickly learn what the command means. You can test this by putting your dog inside the house, burying a dozen or so treats and exciting chew toys, and then letting him out. Say 'Dig in your pit' and praise him if he goes to the pit. This training can usually be accomplished in one afternoon.
Once your dog has learned that digging in his pit is fun; you must still encourage and praise him whenever he shows any interest in the pit and especially for digging there without any encouragement from you. Now, every morning before you leave for work, you can hide all kinds of fun things in the pit, which will keep your dog busy digging for hours. Even if he has found all the goodies, he will keep digging and looking to see if anything else is buried there.
Once your dog understands that digging in his pit is an acceptable and enjoyable activity, and knows the meaning of the command, 'Dig in your pit,' now you can teach him that digging elsewhere is forbidden. The first step is to teach your dog to stay completely off the flower and vegetable gardens and that he is only allowed on the grass or other parts of your yard. Clearly demarcate the forbidden areas with a short fence about a foot high. A few small sticks with a length of string attached along the top are fine. The fence is not intended as a physical barrier but as a means of marking a boundary. Spend the day outside and watch your dog. Each time he gets close to the boundary, quietly warn him by saying 'Off.' If one paw goes over the line, scold him firmly until all four paws are on the correct side of the boundary and then immediately praise him. If you invest some time, your dog will learn to keep out of the garden.