# MatDestroy
Frees space taken by a matrix. 
## Synopsis
```
#include "petscmat.h" 
PetscErrorCode MatDestroy(Mat *A)
```
Collective


## Input Parameter

- ***A -*** the matrix





## Developer Note
Some special arrays of matrices are not destroyed in this routine but instead by the routines called by
`MatDestroySubMatrices()`. Thus one must be sure that any changes here must also be made in those routines.
`MatHeaderMerge()` and `MatHeaderReplace()` also manipulate the data in the `Mat` object and likely need changes
if changes are needed here.


## See Also
 `Mat`, `MatCreate()`

## Level
beginner

## Location
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/interface/matrix.c.html#MatDestroy">src/mat/interface/matrix.c</A>

## Examples
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/tutorials/ex1.c.html">src/mat/tutorials/ex1.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/tutorials/ex10.c.html">src/mat/tutorials/ex10.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/tutorials/ex11.c.html">src/mat/tutorials/ex11.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/tutorials/ex12.c.html">src/mat/tutorials/ex12.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/tutorials/ex15.c.html">src/mat/tutorials/ex15.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/tutorials/ex15f.F90.html">src/mat/tutorials/ex15f.F90.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/tutorials/ex16.c.html">src/mat/tutorials/ex16.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/tutorials/ex17.c.html">src/mat/tutorials/ex17.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/tutorials/ex17f.F90.html">src/mat/tutorials/ex17f.F90.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/tutorials/ex18.c.html">src/mat/tutorials/ex18.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/tutorials/ex2.c.html">src/mat/tutorials/ex2.c.html</A><BR>

## Implementations

<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/dm/impls/da/hypre/mhyp.c.html#MatDestroy_HYPREStruct">MatDestroy_HYPREStruct in src/dm/impls/da/hypre/mhyp.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/dm/impls/da/hypre/mhyp.c.html#MatDestroy_HYPRESStruct">MatDestroy_HYPRESStruct in src/dm/impls/da/hypre/mhyp.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/dm/impls/da/usfft/matusfft.c.html#MatDestroy_SeqUSFFT">MatDestroy_SeqUSFFT in src/dm/impls/da/usfft/matusfft.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/pc/impls/ml/ml.c.html#MatDestroy_ML">MatDestroy_ML in src/ksp/pc/impls/ml/ml.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/adj/mpi/mpiadj.c.html#MatDestroy_MPIAdj">MatDestroy_MPIAdj in src/mat/impls/adj/mpi/mpiadj.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/mpi/clique/clique.cxx.html#MatDestroy_SparseElemental">MatDestroy_SparseElemental in src/mat/impls/aij/mpi/clique/clique.cxx</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/mpi/crl/mcrl.c.html#MatDestroy_MPIAIJCRL">MatDestroy_MPIAIJCRL in src/mat/impls/aij/mpi/crl/mcrl.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/mpi/kokkos/mpiaijkok.kokkos.cxx.html#MatDestroy_MPIAIJKokkos">MatDestroy_MPIAIJKokkos in src/mat/impls/aij/mpi/kokkos/mpiaijkok.kokkos.cxx</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/mpi/mpiaij.c.html#MatDestroy_MPIAIJ">MatDestroy_MPIAIJ in src/mat/impls/aij/mpi/mpiaij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/mpi/mpiaij.c.html#MatDestroy_MatMatMPIAIJBACKEND">MatDestroy_MatMatMPIAIJBACKEND in src/mat/impls/aij/mpi/mpiaij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/mpi/mpicusparse/mpiaijcusparse.cu.html#MatDestroy_MPIAIJCUSPARSE">MatDestroy_MPIAIJCUSPARSE in src/mat/impls/aij/mpi/mpicusparse/mpiaijcusparse.cu</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/mpi/mpihipsparse/mpiaijhipsparse.hip.c.html#MatDestroy_MPIAIJHIPSPARSE">MatDestroy_MPIAIJHIPSPARSE in src/mat/impls/aij/mpi/mpihipsparse/mpiaijhipsparse.hip.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/mpi/mpiviennacl/mpiaijviennacl.cxx.html#MatDestroy_MPIAIJViennaCL">MatDestroy_MPIAIJViennaCL in src/mat/impls/aij/mpi/mpiviennacl/mpiaijviennacl.cxx</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/mpi/mumps/mumps.c.html#MatDestroy_MUMPS">MatDestroy_MUMPS in src/mat/impls/aij/mpi/mumps/mumps.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/mpi/pastix/pastix.c.html#MatDestroy_Pastix">MatDestroy_Pastix in src/mat/impls/aij/mpi/pastix/pastix.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/mpi/strumpack/strumpack.c.html#MatDestroy_STRUMPACK">MatDestroy_STRUMPACK in src/mat/impls/aij/mpi/strumpack/strumpack.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/seq/aij.c.html#MatDestroy_SeqAIJ">MatDestroy_SeqAIJ in src/mat/impls/aij/seq/aij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/seq/aijmkl/aijmkl.c.html#MatDestroy_SeqAIJMKL">MatDestroy_SeqAIJMKL in src/mat/impls/aij/seq/aijmkl/aijmkl.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/seq/aijperm/aijperm.c.html#MatDestroy_SeqAIJPERM">MatDestroy_SeqAIJPERM in src/mat/impls/aij/seq/aijperm/aijperm.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/seq/aijsell/aijsell.c.html#MatDestroy_SeqAIJSELL">MatDestroy_SeqAIJSELL in src/mat/impls/aij/seq/aijsell/aijsell.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/seq/crl/crl.c.html#MatDestroy_SeqAIJCRL">MatDestroy_SeqAIJCRL in src/mat/impls/aij/seq/crl/crl.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/seq/essl/essl.c.html#MatDestroy_Essl">MatDestroy_Essl in src/mat/impls/aij/seq/essl/essl.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/seq/klu/klu.c.html#MatDestroy_KLU">MatDestroy_KLU in src/mat/impls/aij/seq/klu/klu.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/seq/kokkos/aijkok.kokkos.cxx.html#MatDestroy_SeqAIJKokkos">MatDestroy_SeqAIJKokkos in src/mat/impls/aij/seq/kokkos/aijkok.kokkos.cxx</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/seq/lusol/lusol.c.html#MatDestroy_LUSOL">MatDestroy_LUSOL in src/mat/impls/aij/seq/lusol/lusol.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/seq/matlab/aijmatlab.c.html#MatDestroy_matlab">MatDestroy_matlab in src/mat/impls/aij/seq/matlab/aijmatlab.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/seq/seqcusparse/aijcusparse.cu.html#MatDestroy_MatMatCusparse">MatDestroy_MatMatCusparse in src/mat/impls/aij/seq/seqcusparse/aijcusparse.cu</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/seq/seqcusparse/aijcusparse.cu.html#MatDestroy_SeqAIJCUSPARSE">MatDestroy_SeqAIJCUSPARSE in src/mat/impls/aij/seq/seqcusparse/aijcusparse.cu</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/seq/seqhipsparse/aijhipsparse.hip.c.html#MatDestroy_MatMatHipsparse">MatDestroy_MatMatHipsparse in src/mat/impls/aij/seq/seqhipsparse/aijhipsparse.hip.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/seq/seqhipsparse/aijhipsparse.hip.c.html#MatDestroy_SeqAIJHIPSPARSE">MatDestroy_SeqAIJHIPSPARSE in src/mat/impls/aij/seq/seqhipsparse/aijhipsparse.hip.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/seq/seqviennacl/aijviennacl.cxx.html#MatDestroy_SeqAIJViennaCL">MatDestroy_SeqAIJViennaCL in src/mat/impls/aij/seq/seqviennacl/aijviennacl.cxx</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/seq/superlu/superlu.c.html#MatDestroy_SuperLU">MatDestroy_SuperLU in src/mat/impls/aij/seq/superlu/superlu.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/aij/seq/umfpack/umfpack.c.html#MatDestroy_UMFPACK">MatDestroy_UMFPACK in src/mat/impls/aij/seq/umfpack/umfpack.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/baij/mpi/mpibaij.c.html#MatDestroy_MPIBAIJ">MatDestroy_MPIBAIJ in src/mat/impls/baij/mpi/mpibaij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/baij/seq/baij.c.html#MatDestroy_SeqBAIJ">MatDestroy_SeqBAIJ in src/mat/impls/baij/seq/baij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/baij/seq/baijmkl/baijmkl.c.html#MatDestroy_SeqBAIJMKL">MatDestroy_SeqBAIJMKL in src/mat/impls/baij/seq/baijmkl/baijmkl.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/blockmat/seq/blockmat.c.html#MatDestroy_BlockMat">MatDestroy_BlockMat in src/mat/impls/blockmat/seq/blockmat.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/cdiagonal/cdiagonal.c.html#MatDestroy_ConstantDiagonal">MatDestroy_ConstantDiagonal in src/mat/impls/cdiagonal/cdiagonal.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/composite/mcomposite.c.html#MatDestroy_Composite">MatDestroy_Composite in src/mat/impls/composite/mcomposite.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/cufft/cufft.cu.html#MatDestroy_SeqCUFFT">MatDestroy_SeqCUFFT in src/mat/impls/cufft/cufft.cu</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/dense/mpi/mpidense.c.html#MatDestroy_MPIDense">MatDestroy_MPIDense in src/mat/impls/dense/mpi/mpidense.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/dense/seq/cuda/densecuda.cu.html#MatDestroy_SeqDenseCUDA">MatDestroy_SeqDenseCUDA in src/mat/impls/dense/seq/cuda/densecuda.cu</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/dense/seq/dense.c.html#MatDestroy_SeqDense">MatDestroy_SeqDense in src/mat/impls/dense/seq/dense.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/dense/seq/hip/densehip.hip.c.html#MatDestroy_SeqDenseHIP">MatDestroy_SeqDenseHIP in src/mat/impls/dense/seq/hip/densehip.hip.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/dummy/matdummy.c.html#MatDestroy_Dummy">MatDestroy_Dummy in src/mat/impls/dummy/matdummy.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/elemental/matelem.cxx.html#MatDestroy_Elemental">MatDestroy_Elemental in src/mat/impls/elemental/matelem.cxx</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/fft/fft.c.html#MatDestroy_FFT">MatDestroy_FFT in src/mat/impls/fft/fft.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/fft/fftw/fftw.c.html#MatDestroy_FFTW">MatDestroy_FFTW in src/mat/impls/fft/fftw/fftw.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/h2opus/cuda/math2opus.cu.html#MatDestroy_H2OPUS">MatDestroy_H2OPUS in src/mat/impls/h2opus/cuda/math2opus.cu</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/htool/htool.cxx.html#MatDestroy_Htool">MatDestroy_Htool in src/mat/impls/htool/htool.cxx</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/hypre/mhypre.c.html#MatDestroy_HYPRE">MatDestroy_HYPRE in src/mat/impls/hypre/mhypre.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/is/matis.c.html#MatDestroy_IS">MatDestroy_IS in src/mat/impls/is/matis.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/kaij/kaij.c.html#MatDestroy_SeqKAIJ">MatDestroy_SeqKAIJ in src/mat/impls/kaij/kaij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/kaij/kaij.c.html#MatDestroy_MPIKAIJ">MatDestroy_MPIKAIJ in src/mat/impls/kaij/kaij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/localref/mlocalref.c.html#MatDestroy_LocalRef">MatDestroy_LocalRef in src/mat/impls/localref/mlocalref.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/lrc/lrc.c.html#MatDestroy_LRC">MatDestroy_LRC in src/mat/impls/lrc/lrc.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/maij/maij.c.html#MatDestroy_SeqMAIJ">MatDestroy_SeqMAIJ in src/mat/impls/maij/maij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/maij/maij.c.html#MatDestroy_MPIMAIJ">MatDestroy_MPIMAIJ in src/mat/impls/maij/maij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/mffd/mffd.c.html#MatDestroy_MFFD">MatDestroy_MFFD in src/mat/impls/mffd/mffd.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/nest/matnest.c.html#MatDestroy_Nest">MatDestroy_Nest in src/mat/impls/nest/matnest.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/normal/normm.c.html#MatDestroy_Normal">MatDestroy_Normal in src/mat/impls/normal/normm.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/normal/normmh.c.html#MatDestroy_NormalHermitian">MatDestroy_NormalHermitian in src/mat/impls/normal/normmh.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/preallocator/matpreallocator.c.html#MatDestroy_Preallocator">MatDestroy_Preallocator in src/mat/impls/preallocator/matpreallocator.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/sbaij/mpi/mpisbaij.c.html#MatDestroy_MPISBAIJ">MatDestroy_MPISBAIJ in src/mat/impls/sbaij/mpi/mpisbaij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/sbaij/seq/cholmod/sbaijcholmod.c.html#MatDestroy_CHOLMOD">MatDestroy_CHOLMOD in src/mat/impls/sbaij/seq/cholmod/sbaijcholmod.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/sbaij/seq/sbaij.c.html#MatDestroy_SeqSBAIJ">MatDestroy_SeqSBAIJ in src/mat/impls/sbaij/seq/sbaij.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/scalapack/matscalapack.c.html#MatDestroy_ScaLAPACK">MatDestroy_ScaLAPACK in src/mat/impls/scalapack/matscalapack.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/scatter/mscatter.c.html#MatDestroy_Scatter">MatDestroy_Scatter in src/mat/impls/scatter/mscatter.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/sell/mpi/mpisell.c.html#MatDestroy_MPISELL">MatDestroy_MPISELL in src/mat/impls/sell/mpi/mpisell.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/sell/seq/sell.c.html#MatDestroy_SeqSELL">MatDestroy_SeqSELL in src/mat/impls/sell/seq/sell.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/shell/shell.c.html#MatDestroy_Shell">MatDestroy_Shell in src/mat/impls/shell/shell.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/shell/shellcnv.c.html#MatDestroy_CF">MatDestroy_CF in src/mat/impls/shell/shellcnv.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/submat/submat.c.html#MatDestroy_SubMatrix">MatDestroy_SubMatrix in src/mat/impls/submat/submat.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/transpose/htransm.c.html#MatDestroy_HT">MatDestroy_HT in src/mat/impls/transpose/htransm.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/impls/transpose/transm.c.html#MatDestroy_Transpose">MatDestroy_Transpose in src/mat/impls/transpose/transm.c</A><BR>


---
[Edit on GitLab](https://gitlab.com/petsc/petsc/-/edit/release/src/mat/interface/matrix.c)


[Index of all Mat routines](index.md)  
[Table of Contents for all manual pages](/docs/manualpages/index.md)  
[Index of all manual pages](/docs/manualpages/singleindex.md)  
