I'm just I'm trying to destroy the copies of the prefab not the actual prefab

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

public class AaP : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
    if (transform.position.z > 50)
    {
        Destroy(gameObject);
    }
}

}

Where are you instantiating the prefab? Please provide full script so we can help

Scripts only run on game objects in the hierarchy and only then if they are active. A Prefab in your assets folder never has its script run until it’s instantiated.

If, after dragging the prefab to assets, you have left a copy of the prefab in the hierarchy then your script will run as soon as you play the game

If, after dragging the prefab to assets, you have deleted the copy in the hierarchy then you will need to instantiate the prefab and, as soon as you do so, the script will be run.

The problem was that the prefab was also in the hierarchy so I removed it from the hierarchy