# Deleting a container of constraints

**URL:** <https://discourse.julialang.org/t/deleting-a-container-of-constraints/95257>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [February 27, 2023, 9:34am UTC](https://discourse.julialang.org/t/deleting-a-container-of-constraints/95257 "2023-02-27T09:34:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![A\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/a_m/32/46038_2.png) [@A\_M](https://discourse.julialang.org/u/A_M)\
**Post date:** [February 27, 2023, 9:34am UTC](https://discourse.julialang.org/t/deleting-a-container-of-constraints/95257/1 "2023-02-27T09:34:04Z")

</div>

I am trying to remove a container of constraints after solving it to add a new set of constraints before resolving it:  
`@constraint(model, A[s=1:n], x[s] == y[s])`

I tried  
`for s in 1:n delete(model, A[s]) end`  
or  
`delete.(Ref(model),A)`

but I always get this error `JULIA: MethodError: no method matching iterate(::Nothing)`

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [February 27, 2023, 6:51pm UTC](https://discourse.julialang.org/t/deleting-a-container-of-constraints/95257/2 "2023-02-27T18:51:52Z")

</div>

Do you have a reproducible example?

```julia
julia> using JuMP

julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> n = 3
3

julia> y = rand(n)
3-element Vector{Float64}:
 0.7139810928549708
 0.45354593999857173
 0.8483710062233065

julia> @variable(model, x[1:n])
3-element Vector{VariableRef}:
 x[1]
 x[2]
 x[3]

julia> @constraint(model, A[s = 1:n], x[s] == y[s])
3-element Vector{ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64}, MathOptInterface.EqualTo{Float64}}, ScalarShape}}:
 A[1] : x[1] = 0.7139810928549708
 A[2] : x[2] = 0.45354593999857173
 A[3] : x[3] = 0.8483710062233065

julia> delete.(model, A)
3-element Vector{Nothing}:
 nothing
 nothing
 nothing

julia> unregister(model, :A)

julia> @constraint(model, A[s = 1:n], x[s] == y[s])
3-element Vector{ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64}, MathOptInterface.EqualTo{Float64}}, ScalarShape}}:
 A[1] : x[1] = 0.7139810928549708
 A[2] : x[2] = 0.45354593999857173
 A[3] : x[3] = 0.8483710062233065

```

Note the need for `unregister`.

---

<div class="post-metadata">

**Author:** ![A\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/a_m/32/46038_2.png) [@A\_M](https://discourse.julialang.org/u/A_M)\
**Post date:** [February 27, 2023, 11:01pm UTC](https://discourse.julialang.org/t/deleting-a-container-of-constraints/95257/3 "2023-02-27T23:01:57Z")

</div>

Thanks @odow

I am using `direct_model` with MOSEK which I construct a linear approximation for SOCP constraints to provide a hot start for the next run. Then I remove the linear approximation constraints and add a set of new variables and SOCP constraints.

Can I reuse the same model? Or should I create a new one?  
If I have to create a new model, I believe I can’t share the constraints from the original model with the new model; what about variables?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [February 27, 2023, 11:10pm UTC](https://discourse.julialang.org/t/deleting-a-container-of-constraints/95257/4 "2023-02-27T23:10:08Z")

</div>

Do you have a reproducible example with the full error message?

> I am using `direct_model` with MOSEK

I would just use `Model(Mosek.Optimizer)`. `direct_model` has its uses, but it also adds complexity because some things aren’t supported.

> I construct a linear approximation for SOCP constraints

Mosek supports SOCP constraints, so why the need to approximate?
