How do I stop spawning prefabs after a certain amount of time? Also how do i detroy the cloned prefabs created to imporve the performance.

Can someone please help me? Im new to c# and have been folling tutorials mainly, it would be a big help if someone could assist me please.
This is my code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RandomSpawn : MonoBehaviour {

    // prefabs to instantiate
    public GameObject prefab1, prefab2;

    // spawn prefabs once per 2 seconds
    public float spawnRate = 2f;

    // variable to set next spawn
    float nextSpawn = 0f;

    // varaible to contain random value
    int whatToSpawn;

    
        // Update is called once per frame
    void Update() {
        if (Time.time > nextSpawn)
        { // if time has come
            whatToSpawn = Random.Range(1, 3); // define random value between 1 and 5 (6 is exclusive)
            Debug.Log(whatToSpawn);
                        

            // instantiate a prefab depending on random value
            switch (whatToSpawn)
            {
                case 1:
                    Instantiate(prefab1, transform.position, Quaternion.identity);
                    break;
                case 2:
                    Instantiate(prefab2, transform.position, Quaternion.identity);
                    break;           
            }

                     
           
            //set next spawn time
            nextSpawn = Time.time + spawnRate;
        }  
        
    }
}

Use this code to better control the spawning.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RandomSpawn : MonoBehaviour {
 
     // prefabs to instantiate
     public GameObject prefab1, prefab2;
 
     // spawn prefabs once per 2 seconds
     public float spawnRate = 2f;
 
     // variable to stop spawning after spawning a certain number
     int stopAfterSpawns = 5;
     
     // variable to stop spawning after a few seconds
     float stopAfterSeconds = 10;

     // varaible to contain random value
     int whatToSpawn;
 
     float startingTime = 0f;

     void Start(){
          InvokeRepeating("SpawnObject", spawnRate, spawnRate);
          startingTime = Time.time;
     }

     void SpawnObject(){
             if (stopAfterSpawns <= 0 || stopAfterSeconds > Time.time - startingTime){
                    CancelInvoke("SpawnObject");
                    return;
             }
             
             stopAfterSpawns = stopAfterSpawns - 1;

             whatToSpawn = Random.Range(1, 3); // define random value between 1 and 5 (6 is exclusive)
             Debug.Log(whatToSpawn);


             // instantiate a prefab depending on random value
             switch (whatToSpawn)
             {
                 case 1:
                     Instantiate(prefab1, transform.position, Quaternion.identity);
                     break;
                 case 2:
                     Instantiate(prefab2, transform.position, Quaternion.identity);
                     break;           
             }
     }
 } 

Now, to destroy the instantiated objects, I would suggest that you create another script and have it attached to the game object you are instantiating. This script would basically have some logic to destroy the game object. So for example if you want to destroy the instantiated clones after they get instantiated by 5 seconds. Invoke a function to run after 5 seconds that destroys the object.