Can I use a shallow box to install a 240v outlet?
[Assuming USA/Canada location/rules] So long as your box fill is met, yes.
However, there are multiple reasons to not prefer a plastic box, at least one of which relates to box fill - internal clamps count against box fill, while external clamps as more typically used with steel boxes do not count against box fill.
Another is that if this is a large 240VAC outlet as for a range or dryer, the forces involved in plugging and unplugging those are significant, and your plastic box may break, where a steel one would not.
Box fill is: your plastic box should be marked with a number of cubic inches (usually just XY C.I. where X and Y are numbers) - steel boxes have standard volumes which can be looked up in a table. Each wire that terminates in the box adds to the wire count. The receptacle adds 2 to the wire count. Internal clamps add 1 to the wire count. The size (gauge) of each wire corresponds to a number of cubic inches required for each wire &quot;count&quot; of that size - 2.0 for 14 gauge, 2.25 for 12 gauge, 2.5 for 10 gauge, 3.0 for 8 gauge and 5.0 for 6 gauge. The box needs to be at least as large as all those added up (larger is fine) - if you are &quot;depth-limited&quot; you may require a box that is higher and wider than you anticipate, depending on how many and what size wires you are using (i.e. 6 gauge wires for a 50A range receptacle need a lot of room.)
Unlikely to come into play unless your receptacle is &quot;in-line&quot; with another - up to 4 grounding wires are counted as 1 of the largest size grounding wire. After that they are (as of 2020 code) counted as 1/4 (so one counts as 1, 3 count as 1, but 5 count as 1.25)