Start by preparing the spaghetti squash. Carefully cut the squash in half lengthwise and remove the seeds. You can use a spoon to scoop them out.
Add 1 cup of water to the Instant Pot. Place the trivet inside the pot. Arrange the spaghetti squash halves on the trivet, cut side up. Close the lid and seal the valve. Set the Instant Pot to cook on high pressure for 7 minutes.
While the squash is cooking, heat 2 tablespoons of olive oil in a skillet over medium heat. Once hot, add the diced onion and sauté for about 3-4 minutes until translucent.
Add the minced garlic and sliced mushrooms to the skillet. Cook for another 5-7 minutes, stirring occasionally, until the mushrooms are tender.
Stir in the crushed tomatoes, dried oregano, dried basil, salt, pepper, and red pepper flakes (if using) to the mushroom mixture. Allow it to simmer for about 5-10 minutes while the squash finishes cooking.
Once the Instant Pot timer goes off, carefully perform a quick release of the pressure. Open the lid and use a fork to scrape the inside of the spaghetti squash to create spaghetti-like strands.
Serve the spaghetti squash topped with the mushroom ragu. Garnish with fresh basil if desired.